The os module contains an interface to many
operating system-specific functions to manipulate processes, files, file
descriptors, directories and other “low level” features of
the OS. Programs that import and use os stand a
better chance of being portable between different platforms. Portable
programs must depend only on functions that are supported for all
platforms (e.g., unlink and
opendir), and perform all pathname manipulation
The os module exports the following
variables that characterize your operating system.
A name for the operating system, for example
'mac', or 'ce'. Note that
Mac OS X has an os.name of
'posix'; but sys.platform is
'darwin'. Windows XP has an
os.name of 'nt'.
String representing the current
directory ('.', generally).
String representing the parent
directory ('..', generally).
The (or a most common) pathname separator
('/' or ':' or
'\') and the alternate pathname separator
(None or '/'). Most of the
Python library routines will translate '/' to
the correct value for the operating system (typically,
'\' on Windows. It is best to always use
os.path rather than these low-level
The component separator used in $PATH
The line separator in text files ('\n' or
'\015\012'). This is already part of the
The default search path for executables, for example,
The os module has a large number of
functions. Many of these are not directly related to file manipulation.
However, a few are commonly used to create and remove files and
directories. Beyond these basic manipulations, the
shutil module supports a variety of file copy
Change the current working directory to the given path. This
is the directory which the OS uses to transform a relative file
name into an absolute file name.
Returns the path to the current working directory. This is
the directory which the OS use to transform a relative file name
into an absolute file name.
Returns a list of all entries in the
Create the given directory. In GU/Linux, the mode can be
given to specify the permissions; usually this is an octal number.
If not provided, the default of 0777 is used, after being updated
by the OS umask value.
Rename the source filename to the destination filename.
There are a number of errors that can occur if the source file
doesn't exist or the destination file already exists or if the two
paths are on different devices. Each OS handles the situations
Remove (also known as delete or unlink) the file. If you
attempt to remove a directory, this will raise
OSError. If the file is in use, the
standard behavior is to remove the file when it is finally closed;
Windows, however, will raise an exception.
Remove (also known as delete or unlink) the directory. if
you attempt to remove an ordinary file, this will raise
Here's a short example showing some of the functions in the