The os module provides a large set of functions with which we can both query and manipulate the environment within which our program runs, and the file systems within which it operates. The functions are provided in the os module, which we can access by importing os.
Python 2.4.3 (#1, Oct 2 2006, 21:50:13) [GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>>
The following functions can be used to determine or change the environment (in a computer sense) in which our programs run.
os.getpid()
returns the current process' id.os.getppid()
returns the parent process' id.os.getuid()
returns the current process' user
id.os.getpuid()
returns the parent process' user
id.os.getgid()
returns the current process' group
id.os.getpgid()
returns the parent process' group
id.os.getenv(envname[, value])
returns the value of
an environment variable set in the operating system. If 'value' is
specified and no environment variable of that name has been set in
the operating system, then the value of 'value' is returned as a
default.os.chdir(path)
changes the current working
directory to 'path'os.getcwd()
returns a string containing the path
of the current working directory.In addition there are a variety of functions which can be used to manipulate files and directories without opening them. These functions are powerful, and have effects outside of your program, and beyond its runtime. Use them with care, especially rename, remove, and rmdir.
os.chmod(path, mode)
changes the permissions of
'path' to those indicated in 'mode'. 'mode' may be specified as a
decimal or octal value (i.e. a number prefixed with a 0) and it
will act on linux and mac as the chmod command does on the shell.
Otherwise, for greater portability, the os module specifies a
number of constants (available in the documentation) which can be
bitwise or'd together to form a valid mode.os.chown(path, uid, gid)
changes the owning user
and group of 'path' to the user with uid 'uid', and the group with
gid 'gid' respectively.os.listdir(path)
returns a list of the names of
all directories and files (excluding the '.' and '..') in the
directory 'path'.os.mkdir(path)
creates a new directory on disk,
named 'path'. If 'path' is not an absolute path, the directory will
be created in the current working directory.os.remove(path)
deletes the file named 'path' from
disk. If 'path' is not an absolute path, the directory will be
created in the current working directory.os.rmdir(path)
removes the directory named 'path'
from disk, if it is empty. If 'path' is not an absolute path, the
directory will be created in the current working directory.os.rename(source, dest)
renames the file named
'source' to 'dest'. This may have the effect of moving the file
from one directory to another, if 'dest' specifies a file name in
different directory to 'source'. 'dest' cannot be a directory name.
If a file named 'dest' already exists, it will be silently
overwritten.The path manipulation functions in os.path are also very useful. os.path can be accessed as a sub-module of os, by importing os, or directly, by importing os.path, as in
>>> import os.path >>>
os.path.basename(path)
returns a string containing
the last element of 'path' (i.e. the filename or directory name
after the last '/' or '\')os.path.dirname(path)
returns a string containing
everything up to the last '/' or '\' in 'path'os.path.exists(path)
returns True if a file or
directory named 'path' exists, otherwise False.os.path.getsize(path)
returns the size in bytes of
the file or directory named 'path'.os.path.isdir(path)
returns True is 'path' exists
and is a directory, otherwise False.os.path.isfile(path)
returns True is 'path' exists
and is a regular file, otherwise False.