os — Miscellaneous operating system interfaces¶
Source code: Lib/os.py
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.All functions accepting path or file names accept both bytes and string objects, and result in an object of the same type, if a path or file name is returned.
On VxWorks, os.popen, os.fork, os.execv and os.spawn*p* are not supported.
Note
All functions in this module raise OSError (or subclasses thereof) 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','java'.See also
sys.platformhas a finer granularity.os.uname()gives system-dependent version information.The
platformmodule provides detailed checks for the system’s identity.
File Names, Command Line Arguments, and Environment Variables¶
In Python, file names, command line arguments, and environment variables are
represented using the string type. On some systems, decoding these strings to
and from bytes is necessary before passing them to the operating system. Python
uses the filesystem encoding and error handler to perform this
conversion (see sys.getfilesystemencoding()).
The filesystem encoding and error handler are configured at Python
startup by the PyConfig_Read() function: see
filesystem_encoding and
filesystem_errors members of PyConfig.
Changed in version 3.1: On some systems, conversion using the file system encoding may fail. In this case, Python uses the surrogateescape encoding error handler, which means that undecodable bytes are replaced by a Unicode character U+DCxx on decoding, and these are again translated to the original byte on encoding.
The file system encoding must
guarantee to successfully decode all bytes below 128. If the file system
encoding fails to provide this guarantee, API functions can raise
UnicodeError.
See also the locale encoding.
Python UTF-8 Mode¶
New in version 3.7: See PEP 540 for more details.
The Python UTF-8 Mode ignores the locale encoding and forces the usage of the UTF-8 encoding:
Use UTF-8 as the filesystem encoding.
sys.getfilesystemencoding()returns'UTF-8'.locale.getpreferredencoding()returns'UTF-8'(the do_setlocale argument has no effect).sys.stdin,sys.stdout, andsys.stderrall use UTF-8 as their text encoding, with thesurrogateescapeerror handler being enabled forsys.stdinandsys.stdout(sys.stderrcontinues to usebackslashreplaceas it does in the default locale-aware mode)On Unix,
os.device_encoding()returns'UTF-8'rather than the device encoding.
Note that the standard stream settings in UTF-8 mode can be overridden by
PYTHONIOENCODING (just as they can be in the default locale-aware
mode).
As a consequence of the changes in those lower level APIs, other higher level APIs also exhibit different default behaviours:
Command line arguments, environment variables and filenames are decoded to text using the UTF-8 encoding.
os.fsdecode()andos.fsencode()use the UTF-8 encoding.open(),io.open(), andcodecs.open()use the UTF-8 encoding by default. However, they still use the strict error handler by default so that attempting to open a binary file in text mode is likely to raise an exception rather than producing nonsense data.
The Python UTF-8 Mode is enabled if the LC_CTYPE locale is
C or POSIX at Python startup (see the PyConfig_Read()
function).
It can be enabled or disabled using the -X utf8 command line
option and the PYTHONUTF8 environment variable.
If the PYTHONUTF8 environment variable is not set at all, then the
interpreter defaults to using the current locale settings, unless the current
locale is identified as a legacy ASCII-based locale (as described for
PYTHONCOERCECLOCALE), and locale coercion is either disabled or
fails. In such legacy locales, the interpreter will default to enabling UTF-8
mode unless explicitly instructed not to do so.
The Python UTF-8 Mode can only be enabled at the Python startup. Its value
can be read from sys.flags.utf8_mode.
See also the UTF-8 mode on Windows and the filesystem encoding and error handler.
Process Parameters¶
These functions and data items provide information and operate on the current process and user.
-
os.ctermid()¶ Return the filename corresponding to the controlling terminal of the process.
Availability: Unix.
-
os.environ¶ A mapping object where keys and values are strings that represent the process 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.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.On Unix, keys and values use
sys.getfilesystemencoding()and'surrogateescape'error handler. Useenvironbif you would like to use a different encoding.On Windows, the keys are converted to uppercase. This also applies when getting, setting, or deleting an item. For example,
environ['monty'] = 'python'maps the key'MONTY'to the value'python'.Note
Calling
putenv()directly does not changeos.environ, so it’s better to modifyos.environ.Note
On some platforms, including FreeBSD and macOS, setting
environmay cause memory leaks. Refer to the system documentation forputenv().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 3.9: Updated to support PEP 584’s merge (
|) and update (|=) operators.
-
os.environb¶ Bytes version of
environ: a mapping object where both keys and values arebytesobjects representing the process environment.environandenvironbare synchronized (modifyingenvironbupdatesenviron, and vice versa).environbis only available ifsupports_bytes_environisTrue.New in version 3.2.
Changed in version 3.9: Updated to support PEP 584’s merge (
|) and update (|=) operators.
-
os.chdir(path) -
os.fchdir(fd) -
os.getcwd() These functions are described in Files and Directories.
-
os.fsencode(filename)¶ Encode path-like filename to the filesystem encoding and error handler; return
bytesunchanged.fsdecode()is the reverse function.New in version 3.2.
Changed in version 3.6: Support added to accept objects implementing the
os.PathLikeinterface.
-
os.fsdecode(filename)¶ Decode the path-like filename from the filesystem encoding and error handler; return
strunchanged.fsencode()is the reverse function.New in version 3.2.
Changed in version 3.6: Support added to accept objects implementing the
os.PathLikeinterface.
-
os.fspath(path)¶ Return the file system representation of the path.
If
strorbytesis passed in, it is returned unchanged. Otherwise__fspath__()is called and its value is returned as long as it is astrorbytesobject. In all other cases,TypeErroris raised.New in version 3.6.
-
class
os.PathLike¶ An abstract base class for objects representing a file system path, e.g.
pathlib.PurePath.New in version 3.6.
-
os.getenv(key, default=None)¶ Return the value of the environment variable key as a string if it exists, or default if it doesn’t. key is a string. Note that since
getenv()usesos.environ, the mapping ofgetenv()is similarly also captured on import, and the function may not reflect future environment changes.On Unix, keys and values are decoded with
sys.getfilesystemencoding()and'surrogateescape'error handler. Useos.getenvb()if you would like to use a different encoding.Availability: most flavors of Unix, Windows.
-
os.getenvb(key, default=None)¶ Return the value of the environment variable key as bytes if it exists, or default if it doesn’t. key must be bytes. Note that since
getenvb()usesos.environb, the mapping ofgetenvb()is similarly also captured on import, and the function may not reflect future environment changes.getenvb()is only available ifsupports_bytes_environisTrue.Availability: most flavors of Unix.
New in version 3.2.
-
os.get_exec_path(env=None)¶ Returns the list of directories that will be searched for a named executable, similar to a shell, when launching a process. env, when specified, should be an environment variable dictionary to lookup the PATH in. By default, when env is
None,environis used.New in version 3.2.
-
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.getgrouplist(user, group)¶ Return list of group ids that user belongs to. If group is not in the list, it is included; typically, group is specified as the group ID field from the password record for user.
Availability: Unix.
New in version 3.3.
-
os.getgroups()¶ Return list of supplemental group ids associated with the current process.
Availability: Unix.
Note
On macOS,
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.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
getpass.getuser()since the latter checks the environment variablesLOGNAMEorUSERNAMEto find out who the user is, and falls back topwd.getpwuid(os.getuid())[0]to get the login name of the current real user id.Availability: Unix, Windows.
-
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.
-
os.getpgrp()¶ Return the id of the current process group.
Availability: Unix.
-
os.getpid()¶ Return the current process id.
-
os.getppid()¶ Return the parent’s process id. When the parent process has exited, on Unix the id returned is the one of the init process (1), on Windows it is still the same id, which may be already reused by another process.
Availability: Unix, Windows.
Changed in version 3.2: Added support for Windows.
-
os.getpriority(which, who)¶ Get program scheduling priority. The value which is one of
PRIO_PROCESS,PRIO_PGRP, orPRIO_USER, and who is interpreted relative to which (a process identifier forPRIO_PROCESS, process group identifier forPRIO_PGRP, and a user ID forPRIO_USER). A zero value for who denotes (respectively) the calling process, the process group of the calling process, or the real user ID of the calling process.Availability: Unix.
New in version 3.3.
-
os.PRIO_PROCESS¶ -
os.PRIO_PGRP¶ -
os.PRIO_USER¶ Parameters for the
getpriority()andsetpriority()functions.Availability: Unix.
New in version 3.3.
-
os.getresuid()¶ Return a tuple (ruid, euid, suid) denoting the current process’s real, effective, and saved user ids.
Availability: Unix.
New in version 3.2.
-
os.getresgid()¶ Return a tuple (rgid, egid, sgid) denoting the current process’s real, effective, and saved group ids.
Availability: Unix.
New in version 3.2.
-
os.getuid()¶ Return the current process’s real user id.
Availability: Unix.
-
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 3.2.
-
os.putenv(key, value)¶ Set the environment variable named key to the string value. Such changes to the environment affect subprocesses started with
os.system(),popen()orfork()andexecv().Assignments to items in
os.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. This also applies togetenv()andgetenvb(), which respectively useos.environandos.environbin their implementations.Note
On some platforms, including FreeBSD and macOS, setting
environmay cause memory leaks. Refer to the system documentation forputenv().Raises an auditing event
os.putenvwith argumentskey,value.Changed in version 3.9: The function is now always available.
-
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.
Note
On macOS, 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.setpriority(which, who, priority)¶ Set program scheduling priority. The value which is one of
PRIO_PROCESS,PRIO_PGRP, orPRIO_USER, and who is interpreted relative to which (a process identifier forPRIO_PROCESS, process group identifier forPRIO_PGRP, and a user ID forPRIO_USER). A zero value for who denotes (respectively) the calling process, the process group of the calling process, or the real user ID of the calling process. priority is a value in the range -20 to 19. The default priority is 0; lower priorities cause more favorable scheduling.Availability: Unix.
New in version 3.3.
-
os.setregid(rgid, egid)¶ Set the current process’s real and effective group ids.
Availability: Unix.
-
os.setresgid(rgid, egid, sgid)¶ Set the current process’s real, effective, and saved group ids.
Availability: Unix.
New in version 3.2.
-
os.setresuid(ruid, euid, suid)¶ Set the current process’s real, effective, and saved user ids.
Availability: Unix.
New in version 3.2.
-
os.setreuid(ruid, euid)¶ Set the current process’s real and effective user ids.
Availability: Unix.
-
os.getsid(pid)¶ Call the system call
getsid(). See the Unix manual for the semantics.Availability: Unix.
-
os.setsid()¶ Call the system call
setsid(). See the Unix manual for the semantics.Availability: Unix.
-
os.setuid(uid)¶ Set the current process’s user id.
Availability: Unix.
-
os.strerror(code)¶ Return the error message corresponding to the error code in code. On platforms where
strerror()returnsNULLwhen given an unknown error number,ValueErroris raised.
-
os.supports_bytes_environ¶ Trueif the native OS type of the environment is bytes (eg.Falseon Windows).New in version 3.2.
-
os.umask(mask)¶ Set the current numeric umask and return the previous umask.
-
os.uname()¶ Returns information identifying the current operating system. The return value is an object with five attributes:
sysname- operating system namenodename- name of machine on network (implementation-defined)release- operating system releaseversion- operating system versionmachine- hardware identifier
For backwards compatibility, this object is also iterable, behaving like a five-tuple containing
sysname,nodename,release,version, andmachinein that order.Some systems truncate
nodenameto 8 characters or to the leading component; a better way to get the hostname issocket.gethostname()or evensocket.gethostbyaddr(socket.gethostname()).Availability: recent flavors of Unix.
Changed in version 3.3: Return type changed from a tuple to a tuple-like object with named attributes.
-
os.unsetenv(key)¶ Unset (delete) the environment variable named key. Such changes to the environment affect subprocesses started with
os.system(),popen()orfork()andexecv().Deletion of items in
os.environis automatically translated into a corresponding call tounsetenv(); however, calls tounsetenv()don’t updateos.environ, so it is actually preferable to delete items ofos.environ.Raises an auditing event
os.unsetenvwith argumentkey.Changed in version 3.9: The function is now always available and is also available on Windows.
File Object Creation¶
These functions create new file objects. (See also
open() for opening file descriptors.)
File Descriptor Operations¶
These functions operate on I/O streams referenced using file descriptors.
File descriptors are small integers corresponding to a file that has been opened by the current process. For example, standard input is usually file descriptor 0, standard output is 1, and standard error is 2. Further files opened by a process will then be assigned 3, 4, 5, and so forth. The name “file descriptor” is slightly deceptive; on Unix platforms, sockets and pipes are also referenced by file descriptors.
The fileno() method can be used to obtain the file descriptor
associated with a file object when required. Note that using the file
descriptor directly will bypass the file object methods, ignoring aspects such
as internal buffering of data.
-
os.close(fd)¶ Close file descriptor fd.
-
os.closerange(fd_low, fd_high)¶ Close all file descriptors from fd_low (inclusive) to fd_high (exclusive), ignoring errors. Equivalent to (but much faster than):
for fd in range(fd_low, fd_high): try: os.close(fd) except OSError: pass
-
os.copy_file_range(src, dst, count, offset_src=None, offset_dst=None)¶ Copy count bytes from file descriptor src, starting from offset offset_src, to file descriptor dst, starting from offset offset_dst. If offset_src is None, then src is read from the current position; respectively for offset_dst. The files pointed by src and dst must reside in the same filesystem, otherwise an
OSErroris raised witherrnoset toerrno.EXDEV.This copy is done without the additional cost of transferring data from the kernel to user space and then back into the kernel. Additionally, some filesystems could implement extra optimizations. The copy is done as if both files are opened as binary.
The return value is the amount of bytes copied. This could be less than the amount requested.
Availability: Linux kernel >= 4.5 or glibc >= 2.27.
New in version 3.8.
-
os.device_encoding(fd)¶ Return a string describing the encoding of the device associated with fd if it is connected to a terminal; else return
None.On Unix, if the Python UTF-8 Mode is enabled, return
'UTF-8'rather than the device encoding.Changed in version 3.10: On Unix, the function now implements the Python UTF-8 Mode.
-
os.dup(fd)¶ Return a duplicate of file descriptor fd. The new file descriptor is non-inheritable.
On Windows, when duplicating a standard stream (0: stdin, 1: stdout, 2: stderr), the new file descriptor is inheritable.
Changed in version 3.4: The new file descriptor is now non-inheritable.
-
os.dup2(fd, fd2, inheritable=True)¶ Duplicate file descriptor fd to fd2, closing the latter first if necessary. Return fd2. The new file descriptor is inheritable by default or non-inheritable if inheritable is
False.Changed in version 3.4: Add the optional inheritable parameter.
Changed in version 3.7: Return fd2 on success. Previously,
Nonewas always returned.