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'>
The os module also offers functions to find important information about your location or about the process. Note that almost all of these are available only on Unix-like systems, not Windows. I have indicated below where they are available on non-Unix platforms.
- getcwd(): Returns the current working directory.
- getcwdu(): Same as getcwd() except the returned string is in Unicode.
- getegid(): Returns the 'effective group ID'. On Unix, processes may have both effective and real group IDs. Note(): Real and effective IDs are usually the same, but sometimes they are not. If you do not know that you need to address these matters, you probably don't.
- geteuid(): Returns the 'effective user ID'.
- getgid(): Returns the 'real group ID' of the process.
- getgroups(): Returns the real group ID of the process.
- getpgid(pid): Returns the process group ID, taking the process ID (pid) of that process as an argument. If the pid is 0, the process group of the calling process is returned.
- getpgrp(): Returns the ID of the current process group. Note that the process group is not necessarily the same as the group ID of the process. Process groups typically pertain to job control, not group ID.
- getpid(): Returns the real process ID of the current process. This is available under both Unix and Windows.
- getppid(): Returns the process ID of the parent process.
- getsid(pid): Like getpgid(), this takes the pid of a process as an argument. It then returns the process session identifier of that process. If given 0 as an argument, information on the cureent process is returned.
- getuid(): Returns the real user ID of the current process.
More on process IDs can be read on About.com's Linux site.
Along with the os module's get functions are the set functions. Many of the set functions complement the get functions, and one need usually only change the first letter. Most of the system calls are available only on Unix-like platforms, however some may be used on Windows, too.
- setgroups(groups): Set the group access list of the current process. The argument groups is a sequence of integers specifying group identifiers. As this call involves administrative permissions, it can only be called as the root or administrative user on a Unix-like system.
- setgid(gid): Sets the group ID of the current process.
- setpgrp(): Creates a new process group. Where implemented, it functions by passing a system call of either setpgrp() or setpgrp(0, 0). It then returns the ID of the new group.
- setpgid(pid, pgrp): Assigns process ID pid to process group pgrp. If both are equal, the process becomes the process leader for the group. If unequal, the process is added to an existing group. Where 0 is used for pid, the current process is used. If 0 is used for pgrp, the process specified by pid becomes a process group leader.
- setreuid(ruid, euid): Sets the real and effective user ID of the calling process.
- setregid(rgid, egid): Sets the real and effective group ID of the calling process.
- setsid(): Creates a new session and returns its session ID. Session IDs are typically used for terminal devices and may be used to administer the jobs created within them.
- setuid(uid): Sets the real user ID of the current process. Usually, this system call requires administrative (root) privileges.
In addition to getting and setting, Python's os module offers several similar functions for system-level commands. All are available on Unix derivatives. Those available on Windows are marked below.
- chdir(path): Change the current working directory
- chroot(path): Change the root directory of the current process.
- ctermid(): Returns the filename of the control terminal for the current process as a string.
- fchdir(fd): Changes the current working directory to fd, a file descriptor to an opened directory.
- putenv(varname, value): Set the environment variable varname to string value. Changes here affect subprocesses started with os.system(), popen() or fork() and execv(). (available on Unix and Windows) NB: On some platforms (e.g., FreeBSD, Mac OS X), setting environ may cause memory leaks. Check your system's documentation for putenv(). When putenv() is supported, assignments to items in os.environ are automatically translated into corresponding calls to putenv(). Note, however, that calls to putenv() do not update os.environ, so it is actually preferable to assign to items of os.environ.
- strerror(code): Return the error message corresponding to the error code in code. (Available on Unix and Windows)
- umask(mask): Set the current numeric umask and returns the previous umask. (Available on Unix and Windows)
- uname(): Return a tuple of 5 strings containing information identifying the current operating system: sysname, nodename, release, version, machine. Some systems truncate the nodename to 8 characters or to the leading component; a better way to get the hostname is socket.gethostname() or even socket.gethostbyaddr(socket.gethostname()). This is available only on recent flavors of Unix.
- unsetenv(varname): Unsets the environment variable named varname.