Python's os module provides a cross-platform interface to many system-level services. In effect, the os module usually offers a subset of posix without the overhead the superset requires. On Windows systems, it will likely import 'nt'. Mac OS uses 'mac'. You can tell which is used by invoking 'os.name' as described below.
Being familiar with some of the more common functions and variables available through the os module is therefore important for more advanced Python programming.
Four general-purpose variables are defined by os:
- os.environ: Maps current Python environmental variables for access by Python. These variables represent Python's knowledge of its environment. Therefore, they can be changed without effecting the greater runtime environment. For a list of all variables, run the following from a Python shell:
for k in os.environ: print "%s: %s" %(k, os.environ[k])
- os.linesep: The string used to indicate line breaks. On Unix-based systems (Unix, Linux, Mac OS X), this is '\n'. However, if you want to emulate a Windows system, you can change this to '\r\n' to represent the carriage return and newline feed strings used on Windows platforms.
- os.name: The name of the operating system-dependent module imported for OS-related access. Options are: posix, nt, dos, mac, ce, java, os2, or riscos.
- os.path: The OS-dependent module for operations related to filesystem paths. Invoking this without arguments will give you the system path to the OS-dependent module being used. For example, on a Linux system, 'os.path' returns:
<module 'posixpath' from '/usr/lib/python2.5/posixpath.pyc'>
.

