Python “sys” and “os” Modules

Module “sys”

import sys

Check Python path, and count

import sys

print “Path has”, len(sys.path), “members”

print “The members are:”

for member in sys.path:

print member

All imported modules

print sys.modules.keys()

The platform type (linux, Win, Mac, etc.)

print sys.platform

Python version

sys.version

Module “os”

import os

Platform name (UNIX/Linux = posix, Windows = nt)

os.name

Current Working Directory

os.getcwd()

If file or directory

os.path.isfile(“/tmp”)

os.path.isdir(“/tmp”)

Run a shell command

os.system(“ping -c 5 127.0.0.1”)


Commands

  • os.system()                =Executing a shell command
  • os.stat()                       =Get the status of a file
  • os.environ()               =Get the users environment
  • os.chdir()                    =Move focus to a different directory
  • os.getcwd()                =Returns the current working directory
  • os.getgid()                  =Return the real group id of the current process
  • os.getuid()                  =Return the current process’s user id
  • os.getpid()                  =Returns the real process ID of the current process
  • os.getlogin()              =Return the name of the user logged
  • os.access()                  =Check read permissions
  • os.chmod()                 =Change the mode of path to the numeric mode
  • os.chown()                 =Change the owner and group id
  • os.umask(mask)      =Set the current numeric umask
  • os.getsize()                =Get the size of a file
  • os.path.getmtime() =Last time a given directory was modified
  • os.path.getatime()  =Last time a given directory was accessed
  • os.environ()               =Get the users environment
  • os.uname()                 =Return information about the current OS
  • os.chroot(path)        =Change the root directory of the current process to path
  • os.listdir(path)         =List of the entries in the directory given by path
  • os.getloadavg()         =Show queue averaged over the last 1, 5, and 15 minutes
  • os.path.exists()        =Check if a path exists
  • os.walk()                     =Print out all directories, sub-directories and files
  • os.mkdir(path)         =Create a directory named path with numeric mode mode
  • os.makedirs(path)   =Recursive directory creation function
  • os.remove(path)       =Remove (delete) the file path
  • os.removedirs(path) =Remove directories recursively
  • os.rename(src, dst)   =Rename the file or directory src to dst
  • os.rmdir(path)            =Remove (delete) the directory path
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s