15.1. os — Miscellaneous operating system interfaces¶
This module provides a portable way of using operating system dependent
functionality. If you just want to read or write a file see open(), if
you want to manipulate paths, see the os.path module, and if you want to
read all the lines in all the files on the command line see the fileinput
module. For creating temporary files and directories see the tempfile
module, and for high-level file and directory handling see the shutil
module.
Notes on the availability of these functions:
The design of all built-in operating system dependent modules of Python is such that as long as the same functionality is available, it uses the same interface; for example, the function
os.stat(path)returns stat information about path in the same format (which happens to have originated with the POSIX interface).Extensions peculiar to a particular operating system are also available through the
osmodule, but using them is of course a threat to portability.An “Availability: Unix” note means that this function is commonly found on Unix systems. It does not make any claims about its existence on a specific operating system.
If not separately noted, all functions that claim “Availability: Unix” are supported on Mac OS X, which builds on a Unix core.
Note
All functions in this module raise OSError in the case of invalid or
inaccessible file names and paths, or other arguments that have the correct
type, but are not accepted by the operating system.
-
os.name¶ The name of the operating system dependent module imported. The following names have currently been registered:
'posix','nt','os2','ce','java','riscos'.See also
sys.platformhas a finer granularity.os.uname()gives system-dependent version information.The
platformmodule provides detailed checks for the system’s identity.
15.1.1. Process Parameters¶
These functions and data items provide information and operate on the current process and user.
-
os.environ¶ A mapping object representing the string environment. For example,
environ['HOME']is the pathname of your home directory (on some platforms), and is equivalent togetenv("HOME")in C.This mapping is captured the first time the
osmodule is imported, typically during Python startup as part of processingsite.py. Changes to the environment made after this time are not reflected inos.environ, except for changes made by modifyingos.environdirectly.If the platform supports the
putenv()function, this mapping may be used to modify the environment as well as query the environment.putenv()will be called automatically when the mapping is modified.Note
Calling
putenv()directly does not changeos.environ, so it’s better to modifyos.environ.Note
On some platforms, including FreeBSD and Mac OS X, setting
environmay cause memory leaks. Refer to the system documentation forputenv().If
putenv()is not provided, a modified copy of this mapping may be passed to the appropriate process-creation functions to cause child processes to use a modified environment.If the platform supports the
unsetenv()function, you can delete items in this mapping to unset environment variables.unsetenv()will be called automatically when an item is deleted fromos.environ, and when one of thepop()orclear()methods is called.Changed in version 2.6: Also unset environment variables when calling
os.environ.clear()andos.environ.pop().
-
os.chdir(path) -
os.fchdir(fd) -
os.getcwd() These functions are described in Files and Directories.
-
os.ctermid()¶ Return the filename corresponding to the controlling terminal of the process.
Availability: Unix.
-
os.getegid()¶ Return the effective group id of the current process. This corresponds to the “set id” bit on the file being executed in the current process.
Availability: Unix.
-
os.geteuid()¶ Return the current process’s effective user id.
Availability: Unix.
-
os.getgid()¶ Return the real group id of the current process.
Availability: Unix.
-
os.getgroups()¶ Return list of supplemental group ids associated with the current process.
Availability: Unix.
Note
On Mac OS X,
getgroups()behavior differs somewhat from other Unix platforms. If the Python interpreter was built with a deployment target of10.5or earlier,getgroups()returns the list of effective group ids associated with the current user process; this list is limited to a system-defined number of entries, typically 16, and may be modified by calls tosetgroups()if suitably privileged. If built with a deployment target greater than10.5,getgroups()returns the current group access list for the user associated with the effective user id of the process; the group access list may change over the lifetime of the process, it is not affected by calls tosetgroups(), and its length is not limited to 16. The deployment target value,MACOSX_DEPLOYMENT_TARGET, can be obtained withsysconfig.get_config_var().
-
os.initgroups(username, gid)¶ Call the system initgroups() to initialize the group access list with all of the groups of which the specified username is a member, plus the specified group id.
Availability: Unix.
New in version 2.7.
-
os.getlogin()¶ Return the name of the user logged in on the controlling terminal of the process. For most purposes, it is more useful to use the environment variable
LOGNAMEto find out who the user is, orpwd.getpwuid(os.getuid())[0]to get the login name of the process’s real user id.Availability: Unix.
-
os.getpgid(pid)¶ Return the process group id of the process with process id pid. If pid is 0, the process group id of the current process is returned.
Availability: Unix.
New in version 2.3.
-
os.getpgrp()¶ Return the id of the current process group.
Availability: Unix.
-
os.getpid()¶ Return the current process id.
Availability: Unix, Windows.
-
os.getppid()¶ Return the parent’s process id.
Availability: Unix.
-
os.getresuid()¶ Return a tuple (ruid, euid, suid) denoting the current process’s real, effective, and saved user ids.
Availability: Unix.
New in version 2.7.
-
os.getresgid()¶ Return a tuple (rgid, egid, sgid) denoting the current process’s real, effective, and saved group ids.
Availability: Unix.
New in version 2.7.
-
os.getuid()¶ Return the current process’s real user id.
Availability: Unix.
-
os.getenv(varname[, value])¶ Return the value of the environment variable varname if it exists, or value if it doesn’t. value defaults to
None.Availability: most flavors of Unix, Windows.
-
os.putenv(varname, value)¶ Set the environment variable named varname to the string value. Such changes to the environment affect subprocesses started with
os.system(),popen()orfork()andexecv().Availability: most flavors of Unix, Windows.
Note
On some platforms, including FreeBSD and Mac OS X, setting
environmay cause memory leaks. Refer to the system documentation for putenv.When
putenv()is supported, assignments to items inos.environare automatically translated into corresponding calls toputenv(); however, calls toputenv()don’t updateos.environ, so it is actually preferable to assign to items ofos.environ.
-
os.setegid(egid)¶ Set the current process’s effective group id.
Availability: Unix.
-
os.seteuid(euid)¶ Set the current process’s effective user id.
Availability: Unix.
-
os.setgid(gid)¶ Set the current process’ group id.
Availability: Unix.
-
os.setgroups(groups)¶ Set the list of supplemental group ids associated with the current process to groups. groups must be a sequence, and each element must be an integer identifying a group. This operation is typically available only to the superuser.
Availability: Unix.
New in version 2.2.
Note
On Mac OS X, the length of groups may not exceed the system-defined maximum number of effective group ids, typically 16. See the documentation for
getgroups()for cases where it may not return the same group list set by calling setgroups().
-
os.setpgrp()¶ Call the system call
setpgrp()orsetpgrp(0, 0)depending on which version is implemented (if any). See the Unix manual for the semantics.Availability: Unix.
-
os.setpgid(pid, pgrp)¶ Call the system call
setpgid()to set the process group id of the process with id pid to the process group with id pgrp. See the Unix manual for the semantics.Availability: Unix.
-
os.setregid(
