Os Miscellaneous Operating System Interfaces
Os Miscellaneous Operating System Interfaces
docs.python.org/3/library/os.html
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.
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 os
module, 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.
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
1/77
The platform module provides detailed checks for the system’s identity.
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 may raise UnicodeErrors.
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 representing the string environment. For example, environ['HOME'] is the
pathname of your home directory (on some platforms), and is equivalent to
getenv("HOME") in C.
This mapping is captured the first time the os module is imported, typically during Python
startup as part of processing site.py . Changes to the environment made after this time are
not reflected in os.environ , except for changes made by modifying os.environ directly.
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.
2/77
Note
Calling putenv() directly does not change os.environ , so it’s better to modify os.environ .
Note
On some platforms, including FreeBSD and Mac OS X, setting environ may cause memory
leaks. Refer to the system documentation for putenv() .
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
from os.environ , and when one of the pop() or clear() methods is called.
os. environb
Bytes version of environ: a mapping object representing the environment as byte strings.
environ and environb are synchronized (modify environb updates environ, and vice versa).
Changed in version 3.6: Support added to accept objects implementing the os.PathLike
interface.
Changed in version 3.6: Support added to accept objects implementing the os.PathLike
interface.
If str or bytes is passed in, it is returned unchanged. Otherwise __fspath__() is called and its
value is returned as long as it is a str or bytes object. In all other cases, TypeError is raised.
abstractmethod __fspath__ ()
Return the file system path representation of the object.
The method should only return a str or bytes object, with the preference being for str.
4/77
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 , environ is used.
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.
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 of 10.5 or 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 to
setgroups() if suitably privileged. If built with a deployment target greater than 10.5 ,
getgroups() returns the current group access list for the user associated with the effective
5/77
user id of the process; the group access list may change over the lifetime of the process, it is
not affected by calls to setgroups(), and its length is not limited to 16. The deployment target
value, MACOSX_DEPLOYMENT_TARGET , can be obtained with sysconfig.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
variables LOGNAME or USERNAME to find out who the user is, and falls back to
pwd.getpwuid(os.getuid())[0] to get the login name of the current real user id.
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.
Availability: Unix.
os. getresuid ()
Return a tuple (ruid, euid, suid) denoting the current process’s real, effective, and saved user
ids.
Availability: Unix.
os. getresgid ()
Return a tuple (rgid, egid, sgid) denoting the current process’s real, effective, and saved
group ids.
Availability: Unix.
os. getuid ()
Return the current process’s real user id.
Availability: Unix.
Availability: Unix.
Note
7/77
On some platforms, including FreeBSD and Mac OS X, setting environ may cause memory
leaks. Refer to the system documentation for putenv.
Availability: Unix.
Availability: Unix.
Availability: Unix.
Availability: Unix.
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() or setpgrp(0, 0) depending on which version is implemented
(if any). See the Unix manual for the semantics.
Availability: Unix.
8/77
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.
Availability: Unix.
Availability: Unix.
Availability: Unix.
Availability: Unix.
Availability: Unix.
Availability: Unix.
9/77
os. setsid ()
Call the system call setsid() . See the Unix manual for the semantics.
Availability: Unix.
Availability: Unix.
os. supports_bytes_environ
True if the native OS type of the environment is bytes (eg. False on Windows).
os. uname ()
Returns information identifying the current operating system. The return value is an object
with five attributes:
For backwards compatibility, this object is also iterable, behaving like a five-tuple containing
sysname , nodename , release , version , and machine in that order.
10/77
Changed in version 3.3: Return type changed from a tuple to a tuple-like object with named
attributes.
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.
Note
11/77
This function is intended for low-level I/O and must be applied to a file descriptor as
returned by os.open() or pipe(). To close a “file object” returned by the built-in function open()
or by popen() or fdopen(), use its close() method.
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.
On Windows, when duplicating a standard stream (0: stdin, 1: stdout, 2: stderr), the new file
descriptor is inheritable.
12/77
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.7: Return fd2 on success. Previously, None was always returned.
Availability: Unix.
Raises an auditing event os.chown with arguments path , uid , gid , dir_fd .
Availability: Unix.
Availability: Unix.
Note
If name is a string and is not known, ValueError is raised. If a specific value for name is not
supported by the host system, even if it is included in pathconf_names , an OSError is raised
with errno.EINVAL for the error number.
13/77
As of Python 3.3, this is equivalent to os.pathconf(fd, name) .
Availability: Unix.
See also
Availability: Unix.
If you’re starting with a buffered Python file object f, first do f.flush() , and then do
os.fsync(f.fileno()) , to ensure that all internal buffers associated with f are written to disk.
Availability: Unix.
14/77
New in version 3.5.
Availability: Unix.
os. F_LOCK
os. F_TLOCK
os. F_ULOCK
os. F_TEST
Flags that specify what action lockf() will take.
Availability: Unix.
os. SEEK_SET
os. SEEK_CUR
os. SEEK_END
Parameters to the lseek() function. Their values are 0, 1, and 2, respectively.
New in version 3.3: Some operating systems could support additional values, like
os.SEEK_HOLE or os.SEEK_DATA .
15/77
For a description of the flag and mode values, see the C run-time documentation; flag
constants (like O_RDONLY and O_WRONLY) are defined in the os module. In particular, on
Windows adding O_BINARY is needed to open files in binary mode.
This function can support paths relative to directory descriptors with the dir_fd parameter.
Note
This function is intended for low-level I/O. For normal usage, use the built-in function open() ,
which returns a file object with read() and write() methods (and many more). To wrap a
file descriptor in a file object, use fdopen().
Changed in version 3.5: If the system call is interrupted and the signal handler does not
raise an exception, the function now retries the system call instead of raising an
InterruptedError exception (see PEP 475 for the rationale).
The following constants are options for the flags parameter to the open() function. They can
be combined using the bitwise OR operator | . Some of them are not available on all
platforms. For descriptions of their availability and use, consult the open(2) manual page on
Unix or the MSDN on Windows.
os. O_RDONLY
os. O_WRONLY
os. O_RDWR
os. O_APPEND
os. O_CREAT
os. O_EXCL
os. O_TRUNC
The above constants are available on Unix and Windows.
os. O_DSYNC
os. O_RSYNC
os. O_SYNC
os. O_NDELAY
os. O_NONBLOCK
os. O_NOCTTY
os. O_CLOEXEC
The above constants are only available on Unix.
16/77
Changed in version 3.3: Add O_CLOEXEC constant.
os. O_BINARY
os. O_NOINHERIT
os. O_SHORT_LIVED
os. O_TEMPORARY
os. O_RANDOM
os. O_SEQUENTIAL
os. O_TEXT
The above constants are only available on Windows.
os. O_ASYNC
os. O_DIRECT
os. O_DIRECTORY
os. O_NOFOLLOW
os. O_NOATIME
os. O_PATH
os. O_TMPFILE
os. O_SHLOCK
os. O_EXLOCK
The above constants are extensions and not present if they are not defined by the C library.
Changed in version 3.4: Add O_PATH on systems that support it. Add O_TMPFILE, only
available on Linux Kernel 3.11 or newer.
os. openpty ()
Open a new pseudo-terminal pair. Return a pair of file descriptors (master, slave) for the
pty and the tty, respectively. The new file descriptors are non-inheritable. For a (slightly)
more portable approach, use the pty module.
Changed in version 3.4: The new file descriptors are now non-inheritable.
os. pipe ()
Create a pipe. Return a pair of file descriptors (r, w) usable for reading and writing,
respectively. The new file descriptor is non-inheritable.
Changed in version 3.4: The new file descriptors are now non-inheritable.
17/77
Availability: some flavors of Unix.
Availability: Unix.
Availability: Unix.
os. POSIX_FADV_NORMAL
os. POSIX_FADV_SEQUENTIAL
os. POSIX_FADV_RANDOM
os. POSIX_FADV_NOREUSE
os. POSIX_FADV_WILLNEED
os. POSIX_FADV_DONTNEED
Flags that can be used in advice in posix_fadvise() that specify the access pattern that is likely
to be used.
Availability: Unix.
Return a bytestring containing the bytes read. If the end of the file referred to by fd has
been reached, an empty bytes object is returned.
Availability: Unix.
The flags argument contains a bitwise OR of zero or more of the following flags:
RWF_HIPRI
RWF_NOWAIT
Return the total number of bytes actually read which can be less than the total capacity of
all the objects.
The operating system may set a limit ( sysconf() value 'SC_IOV_MAX' ) on the number of
buffers that can be used.
Availability: Linux 2.6.30 and newer, FreeBSD 6.0 and newer, OpenBSD 2.7 and newer. Using
flags requires Linux 4.6 or newer.
os. RWF_NOWAIT
Do not wait for data which is not immediately available. If this flag is specified, the system
call will return instantly if it would have to read data from the backing storage or wait for a
lock.
If some data was successfully read, it will return the number of bytes read. If no bytes were
read, it will return -1 and set errno to errno.EAGAIN .
os. RWF_HIPRI
High priority read/write. Allows block-based filesystems to use polling of the device, which
provides lower latency, but may use additional resources.
Currently, on Linux, this feature is usable only on a file descriptor opened using the
O_DIRECT flag.
Availability: Unix.
The flags argument contains a bitwise OR of zero or more of the following flags:
RWF_DSYNC
RWF_SYNC
The operating system may set a limit ( sysconf() value 'SC_IOV_MAX' ) on the number of
buffers that can be used.
Availability: Linux 2.6.30 and newer, FreeBSD 6.0 and newer, OpenBSD 2.7 and newer. Using
flags requires Linux 4.7 or newer.
os. RWF_DSYNC
Provide a per-write equivalent of the O_DSYNC open(2) flag. This flag effect applies only to
the data range written by the system call.
os. RWF_SYNC
Provide a per-write equivalent of the O_SYNC open(2) flag. This flag effect applies only to
the data range written by the system call.
20/77
Availability: Linux 4.7 and newer.
Return a bytestring containing the bytes read. If the end of the file referred to by fd has
been reached, an empty bytes object is returned.
Note
This function is intended for low-level I/O and must be applied to a file descriptor as
returned by os.open() or pipe(). To read a “file object” returned by the built-in function open()
or by popen() or fdopen(), or sys.stdin, use its read() or readline() methods.
Changed in version 3.5: If the system call is interrupted and the signal handler does not
raise an exception, the function now retries the system call instead of raising an
InterruptedError exception (see PEP 475 for the rationale).
The first function notation is supported by all platforms that define sendfile().
On Linux, if offset is given as None , the bytes are read from the current position of in and
the position of in is updated.
The second case may be used on Mac OS X and FreeBSD where headers and trailers are
arbitrary sequences of buffers that are written before and after the data from in is written.
It returns the same as the first case.
On Mac OS X and FreeBSD, a value of 0 for count specifies to send until the end of in is
reached.
All platforms support sockets as out file descriptor, and some platforms allow other types
(e.g. regular file, pipe) as well.
Cross-platform applications should not use headers, trailers and flags arguments.
Availability: Unix.
Note
21/77
For a higher-level wrapper of sendfile(), see socket.socket.sendfile().
Availability: Unix.
os. SF_NODISKIO
os. SF_MNOWAIT
os. SF_SYNC
Parameters to the sendfile() function, if the implementation supports them.
Availability: Unix.
Return the total number of bytes actually read which can be less than the total capacity of
all the objects.
The operating system may set a limit ( sysconf() value 'SC_IOV_MAX' ) on the number of
buffers that can be used.
Availability: Unix.
Availability: Unix.
22/77
Set the process group associated with the terminal given by fd (an open file descriptor as
returned by os.open()) to pg.
Availability: Unix.
Availability: Unix.
Note
This function is intended for low-level I/O and must be applied to a file descriptor as
returned by os.open() or pipe(). To write a “file object” returned by the built-in function open()
or by popen() or fdopen(), or sys.stdout or sys.stderr, use its write() method.
Changed in version 3.5: If the system call is interrupted and the signal handler does not
raise an exception, the function now retries the system call instead of raising an
InterruptedError exception (see PEP 475 for the rationale).
The operating system may set a limit ( sysconf() value 'SC_IOV_MAX' ) on the number of
buffers that can be used.
Availability: Unix.
23/77
Return the size of the terminal window as (columns, lines) , tuple of type terminal_size.
columns
Width of the terminal window in characters.
lines
Height of the terminal window in characters.
A file descriptor has an “inheritable” flag which indicates if the file descriptor can be
inherited by child processes. Since Python 3.4, file descriptors created by Python are non-
inheritable by default.
On UNIX, non-inheritable file descriptors are closed in child processes at the execution of a
new program, other file descriptors are inherited.
On Windows, non-inheritable handles and file descriptors are closed in child processes,
except for standard streams (file descriptors 0, 1 and 2: stdin, stdout and stderr), which are
always inherited. Using spawn* functions, all inheritable handles and all inheritable file
descriptors are inherited. Using the subprocess module, all file descriptors except standard
streams are closed, and inheritable handles are only inherited if the close_fds parameter is
False .
24/77
os. get_handle_inheritable (handle)
Get the “inheritable” flag of the specified handle (a boolean).
Availability: Windows.
Availability: Windows.
specifying a file descriptor: Normally the path argument provided to functions in the
os module must be a string specifying a file path. However, some functions now
alternatively accept an open file descriptor for their path argument. The function will
then operate on the file referred to by the descriptor. (For POSIX systems, Python will
call the variant of the function prefixed with f (e.g. call fchdir instead of chdir ).)
You can check whether or not path can be specified as a file descriptor for a particular
function on your platform using os.supports_fd. If this functionality is unavailable, using
it will raise a NotImplementedError.
If the function also supports dir_fd or follow_symlinks arguments, it’s an error to specify
one of those when supplying path as a file descriptor.
You can check whether or not dir_fd is supported for a particular function on your
platform using os.supports_dir_fd . If it’s unavailable, using it will raise a
NotImplementedError.
25/77
not following symlinks: If follow_symlinks is False , and the last element of the path
to operate on is a symbolic link, the function will operate on the symbolic link itself
rather than the file pointed to by the link. (For POSIX systems, Python will call the l...
variant of the function.)
You can check whether or not follow_symlinks is supported for a particular function on
your platform using os.supports_follow_symlinks. If it’s unavailable, using it will raise a
NotImplementedError.
This function can support specifying paths relative to directory descriptors and not following
symlinks.
If effective_ids is True , access() will perform its access checks using the effective uid/gid
instead of the real uid/gid. effective_ids may not be supported on your platform; you can
check whether or not it is available using os.supports_effective_ids. If it is unavailable, using it
will raise a NotImplementedError.
Note
Using access() to check if a user is authorized to e.g. open a file before actually doing so
using open() creates a security hole, because the user might exploit the short time interval
between checking and opening the file to manipulate it. It’s preferable to use EAFP
techniques. For example:
if os.access("myfile", os.R_OK):
with open("myfile") as fp:
return fp.read()
return "some default data"
try:
fp = open("myfile")
except PermissionError:
return "some default data"
else:
with fp:
return fp.read()
26/77
Note
I/O operations may fail even when access() indicates that they would succeed, particularly
for operations on network filesystems which may have permissions semantics beyond the
usual POSIX permission-bit model.
Changed in version 3.3: Added the dir_fd, effective_ids, and follow_symlinks parameters.
os. F_OK
os. R_OK
os. W_OK
os. X_OK
Values to pass as the mode parameter of access() to test the existence, readability, writability
and executability of path, respectively.
This function can support specifying a file descriptor. The descriptor must refer to an
opened directory, not an open file.
This function can raise OSError and subclasses such as FileNotFoundError, PermissionError,
and NotADirectoryError.
New in version 3.3: Added support for specifying path as a file descriptor on some platforms.
stat.UF_NODUMP
stat.UF_IMMUTABLE
stat.UF_APPEND
stat.UF_OPAQUE
stat.UF_NOUNLINK
stat.UF_COMPRESSED
27/77
stat.UF_HIDDEN
stat.SF_ARCHIVED
stat.SF_IMMUTABLE
stat.SF_APPEND
stat.SF_NOUNLINK
stat.SF_SNAPSHOT
Availability: Unix.
stat.S_ISUID
stat.S_ISGID
stat.S_ENFMT
stat.S_ISVTX
stat.S_IREAD
stat.S_IWRITE
stat.S_IEXEC
stat.S_IRWXU
stat.S_IRUSR
stat.S_IWUSR
stat.S_IXUSR
28/77
stat.S_IRWXG
stat.S_IRGRP
stat.S_IWGRP
stat.S_IXGRP
stat.S_IRWXO
stat.S_IROTH
stat.S_IWOTH
stat.S_IXOTH
This function can support specifying a file descriptor, paths relative to directory descriptors
and not following symlinks.
Note
Although Windows supports chmod(), you can only set the file’s read-only flag with it (via the
stat.S_IWRITE and stat.S_IREAD constants or a corresponding integer value). All other bits
are ignored.
New in version 3.3: Added support for specifying path as an open file descriptor, and the
dir_fd and follow_symlinks arguments.
This function can support specifying a file descriptor, paths relative to directory descriptors
and not following symlinks.
See shutil.chown() for a higher-level function that accepts names in addition to numeric ids.
Raises an auditing event os.chown with arguments path , uid , gid , dir_fd .
Availability: Unix.
New in version 3.3: Added support for specifying path as an open file descriptor, and the
dir_fd and follow_symlinks arguments.
29/77
Changed in version 3.6: Supports a path-like object.
Availability: Unix.
Availability: Unix.
os. getcwd ()
Return a string representing the current working directory.
os. getcwdb ()
Return a bytestring representing the current working directory.
Changed in version 3.8: The function now uses the UTF-8 encoding on Windows, rather than
the ANSI code page: see PEP 529 for the rationale. The function is no longer deprecated on
Windows.
Availability: Unix.
Availability: Unix.
30/77
Changed in version 3.6: Accepts a path-like object.
Raises an auditing event os.chown with arguments path , uid , gid , dir_fd .
Availability: Unix.
This function can support specifying src_dir_fd and/or dst_dir_fd to supply paths relative to
directory descriptors, and not following symlinks.
Raises an auditing event os.link with arguments src , dst , src_dir_fd , dst_dir_fd .
New in version 3.3: Added the src_dir_fd, dst_dir_fd, and follow_symlinks arguments.
Changed in version 3.6: Accepts a path-like object for src and dst.
path may be a path-like object. If path is of type bytes (directly or indirectly through the
PathLike interface), the filenames returned will also be of type bytes ; in all other
circumstances, they will be of type str .
This function can also support specifying a file descriptor; the file descriptor must refer to a
directory.
Note
The scandir() function returns directory entries along with file attribute information, giving
better performance for many common use cases.
New in version 3.3: Added support for specifying path as an open file descriptor.
On platforms that do not support symbolic links, this is an alias for stat().
See also
Changed in version 3.2: Added support for Windows 6.0 (Vista) symbolic links.
Changed in version 3.6: Accepts a path-like object for src and dst.
Changed in version 3.8: On Windows, now opens reparse points that represent another
path (name surrogates), including symbolic links and directory junctions. Other kinds of
reparse points are resolved by the operating system as for stat().
On some systems, mode is ignored. Where it is used, the current umask value is first
masked out. If bits other than the last 9 (i.e. the last 3 digits of the octal representation of
the mode) are set, their meaning is platform-dependent. On some platforms, they are
ignored and you should call chmod() explicitly to set them.
32/77
It is also possible to create temporary directories; see the tempfile module’s
tempfile.mkdtemp() function.
The mode parameter is passed to mkdir() for creating the leaf directory; see the mkdir()
description for how it is interpreted. To set the file permission bits of any newly-created
parent directories you can set the umask before invoking makedirs(). The file permission bits
of existing parent directories are not changed.
If exist_ok is False (the default), an FileExistsError is raised if the target directory already
exists.
Note
makedirs() will become confused if the path elements to create include pardir (eg. “..” on
UNIX systems).
Changed in version 3.4.1: Before Python 3.4.1, if exist_ok was True and the directory
existed, makedirs() would still raise an error if mode did not match the mode of the existing
directory. Since this behavior was impossible to implement safely, it was removed in Python
3.4.1. See bpo-21082.
Changed in version 3.7: The mode argument no longer affects the file permission bits of
newly-created intermediate-level directories.
33/77
This function can also support paths relative to directory descriptors.
FIFOs are pipes that can be accessed like regular files. FIFOs exist until they are deleted (for
example with os.unlink()). Generally, FIFOs are used as rendezvous between “client” and
“server” type processes: the server opens the FIFO for reading, and the client opens it for
writing. Note that mkfifo() doesn’t open the FIFO — it just creates the rendezvous point.
Availability: Unix.
Availability: Unix.
34/77
others). Some platforms define additional names as well. The names known to the host
operating system are given in the pathconf_names dictionary. For configuration variables
not included in that mapping, passing an integer for name is also accepted.
If name is a string and is not known, ValueError is raised. If a specific value for name is not
supported by the host system, even if it is included in pathconf_names , an OSError is raised
with errno.EINVAL for the error number.
Availability: Unix.
os. pathconf_names
Dictionary mapping names accepted by pathconf() and fpathconf() to the integer values
defined for those names by the host operating system. This can be used to determine the
set of names known to the system.
Availability: Unix.
If the path is a string object (directly or indirectly through a PathLike interface), the result will
also be a string object, and the call may raise a UnicodeDecodeError. If the path is a bytes
object (direct or indirectly), the result will be a bytes object.
When trying to resolve a path that may contain links, use realpath() to properly handle
recursion and platform differences.
Changed in version 3.2: Added support for Windows 6.0 (Vista) symbolic links.
Changed in version 3.8: Accepts a path-like object and a bytes object on Windows.
35/77
Changed in version 3.8: Added support for directory junctions, and changed to return the
substitution path (which typically includes \\?\ prefix) rather than the optional “print name”
field that was previously returned.
36/77
it will be replaced silently if the user has permission. The operation may fail on some Unix
flavors if src and dst are on different filesystems. If successful, the renaming will be an
atomic operation (this is a POSIX requirement).
This function can support specifying src_dir_fd and/or dst_dir_fd to supply paths relative to
directory descriptors.
Raises an auditing event os.rename with arguments src , dst , src_dir_fd , dst_dir_fd .
Changed in version 3.6: Accepts a path-like object for src and dst.
Note
This function can fail with the new directory structure made if you lack permissions needed
to remove the leaf directory or file.
Raises an auditing event os.rename with arguments src , dst , src_dir_fd , dst_dir_fd .
Changed in version 3.6: Accepts a path-like object for old and new.
This function can support specifying src_dir_fd and/or dst_dir_fd to supply paths relative to
directory descriptors.
Raises an auditing event os.rename with arguments src , dst , src_dir_fd , dst_dir_fd .
Changed in version 3.6: Accepts a path-like object for src and dst.
Using scandir() instead of listdir() can significantly increase the performance of code that also
needs file type or file attribute information, because os.DirEntry objects expose this
information if the operating system provides it when scanning a directory. All os.DirEntry
methods may perform a system call, but is_dir() and is_file() usually only require a system
call for symbolic links; os.DirEntry.stat() always requires a system call on Unix but only
requires one for symbolic links on Windows.
path may be a path-like object. If path is of type bytes (directly or indirectly through the
PathLike interface), the type of the name and path attributes of each os.DirEntry will be
bytes ; in all other circumstances, they will be of type str .
This function can also support specifying a file descriptor; the file descriptor must refer to a
directory.
The scandir() iterator supports the context manager protocol and has the following method:
scandir. close ()
Close the iterator and free acquired resources.
This is called automatically when the iterator is exhausted or garbage collected, or when an
error happens during iterating. However it is advisable to call it explicitly or use the with
statement.
38/77
The following example shows a simple use of scandir() to display all the files (excluding
directories) in the given path that don’t start with '.' . The entry.is_file() call will generally
not make an additional system call:
Note
On Unix-based systems, scandir() uses the system’s opendir() and readdir() functions. On
Windows, it uses the Win32 FindFirstFileW and FindNextFileW functions.
New in version 3.6: Added support for the context manager protocol and the close()
method. If a scandir() iterator is neither exhausted nor explicitly closed a ResourceWarning
will be emitted in its destructor.
scandir() will provide as much of this information as possible without making additional
system calls. When a stat() or lstat() system call is made, the os.DirEntry object will
cache the result.
os.DirEntry instances are not intended to be stored in long-lived data structures; if you
know the file metadata has changed or if a long time has elapsed since calling scandir(), call
os.stat(entry.path) to fetch up-to-date information.
Because the os.DirEntry methods can make operating system calls, they may also raise
OSError. If you need very fine-grained control over errors, you can catch OSError when calling
one of the os.DirEntry methods and handle as appropriate.
name
The entry’s base filename, relative to the scandir() path argument.
39/77
The name attribute will be bytes if the scandir() path argument is of type bytes and str
otherwise. Use fsdecode() to decode byte filenames.
path
The entry’s full path name: equivalent to os.path.join(scandir_path, entry.name) where
scandir_path is the scandir() path argument. The path is only absolute if the scandir() path
argument was absolute. If the scandir() path argument was a file descriptor, the path
attribute is the same as the name attribute.
The path attribute will be bytes if the scandir() path argument is of type bytes and str
otherwise. Use fsdecode() to decode byte filenames.
inode ()
Return the inode number of the entry.
On the first, uncached call, a system call is required on Windows but not on Unix.
If follow_symlinks is False , return True only if this entry is a directory (without following
symlinks); return False if the entry is any other kind of file or if it doesn’t exist anymore.
The result is cached on the os.DirEntry object, with a separate cache for follow_symlinks
True and False . Call os.stat() along with stat.S_ISDIR() to fetch up-to-date information.
On the first, uncached call, no system call is required in most cases. Specifically, for non-
symlinks, neither Windows or Unix require a system call, except on certain Unix file systems,
such as network file systems, that return dirent.d_type == DT_UNKNOWN . If the entry is a
symlink, a system call will be required to follow the symlink unless follow_symlinks is False .
This method can raise OSError, such as PermissionError, but FileNotFoundError is caught and
not raised.
If follow_symlinks is False , return True only if this entry is a file (without following
symlinks); return False if the entry is a directory or other non-file entry, or if it doesn’t exist
anymore.
40/77
The result is cached on the os.DirEntry object. Caching, system calls made, and exceptions
raised are as per is_dir().
is_symlink ()
Return True if this entry is a symbolic link (even if broken); return False if the entry points
to a directory or any kind of file, or if it doesn’t exist anymore.
The result is cached on the os.DirEntry object. Call os.path.islink() to fetch up-to-date
information.
On the first, uncached call, no system call is required in most cases. Specifically, neither
Windows or Unix require a system call, except on certain Unix file systems, such as network
file systems, that return dirent.d_type == DT_UNKNOWN .
This method can raise OSError, such as PermissionError, but FileNotFoundError is caught and
not raised.
On Unix, this method always requires a system call. On Windows, it only requires a system
call if follow_symlinks is True and the entry is a reparse point (for example, a symbolic link
or directory junction).
On Windows, the st_ino , st_dev and st_nlink attributes of the stat_result are always set to
zero. Call os.stat() to get these attributes.
The result is cached on the os.DirEntry object, with a separate cache for follow_symlinks
True and False . Call os.stat() to fetch up-to-date information.
Note that there is a nice correspondence between several attributes and methods of
os.DirEntry and of pathlib.Path. In particular, the name attribute has the same meaning, as
do the is_dir() , is_file() , is_symlink() and stat() methods.
Changed in version 3.6: Added support for the PathLike interface. Added support for bytes
paths on Windows.
41/77
This function normally follows symlinks; to stat a symlink add the argument
follow_symlinks=False , or use lstat() .
This function can support specifying a file descriptor and not following symlinks.
Example:
>>>
>>> import os
>>> statinfo = os.stat('somefile.txt')
>>> statinfo
os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026,
st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295,
st_mtime=1297230027, st_ctime=1297230027)
>>> statinfo.st_size
264
See also
New in version 3.3: Added the dir_fd and follow_symlinks arguments, specifying a file
descriptor instead of a path.
Changed in version 3.8: On Windows, all reparse points that can be resolved by the
operating system are now followed, and passing follow_symlinks=False disables following
all name surrogate reparse points. If the operating system reaches a reparse point that it is
not able to follow, stat now returns the information for the original path as if
follow_symlinks=False had been specified instead of raising an error.
42/77
Attributes:
st_mode
File mode: file type and file mode bits (permissions).
st_ino
Platform dependent, but if non-zero, uniquely identifies the file for a given value of st_dev .
Typically:
st_dev
Identifier of the device on which this file resides.
st_nlink
Number of hard links.
st_uid
User identifier of the file owner.
st_gid
Group identifier of the file owner.
st_size
Size of the file in bytes, if it is a regular file or a symbolic link. The size of a symbolic link is
the length of the pathname it contains, without a terminating null byte.
Timestamps:
st_atime
Time of most recent access expressed in seconds.
st_mtime
Time of most recent content modification expressed in seconds.
st_ctime
Platform dependent:
st_atime_ns
Time of most recent access expressed in nanoseconds as an integer.
st_mtime_ns
43/77
Time of most recent content modification expressed in nanoseconds as an integer.
st_ctime_ns
Platform dependent:
Note
The exact meaning and resolution of the st_atime, st_mtime, and st_ctime attributes depend
on the operating system and the file system. For example, on Windows systems using the
FAT or FAT32 file systems, st_mtime has 2-second resolution, and st_atime has only 1-day
resolution. See your operating system documentation for details.
On some Unix systems (such as Linux), the following attributes may also be available:
st_blocks
Number of 512-byte blocks allocated for file. This may be smaller than st_size /512 when the
file has holes.
st_blksize
“Preferred” blocksize for efficient file system I/O. Writing to a file in smaller chunks may
cause an inefficient read-modify-rewrite.
st_rdev
Type of device if an inode device.
st_flags
User defined flags for file.
On other Unix systems (such as FreeBSD), the following attributes may be available (but may
be only filled out if root tries to use them):
st_gen
File generation number.
st_birthtime
Time of file creation.
44/77
On Solaris and derivatives, the following attributes may also be available:
st_fstype
String that uniquely identifies the type of the filesystem that contains the file.
st_rsize
Real size of the file.
st_creator
Creator of the file.
st_type
File type.
st_file_attributes
Windows file attributes: dwFileAttributes member of the BY_HANDLE_FILE_INFORMATION
structure returned by GetFileInformationByHandle() . See the FILE_ATTRIBUTE_* constants
in the stat module.
st_reparse_tag
When st_file_attributes has the FILE_ATTRIBUTE_REPARSE_POINT set, this field contains the
tag identifying the type of reparse point. See the IO_REPARSE_TAG_* constants in the stat
module.
The standard module stat defines functions and constants that are useful for extracting
information from a stat structure. (On Windows, some items are filled with dummy
values.)
New in version 3.3: Added the st_atime_ns, st_mtime_ns, and st_ctime_ns members.
Changed in version 3.5: Windows now returns the file index as st_ino when available.
45/77
New in version 3.8: Added the st_reparse_tag member on Windows.
Changed in version 3.8: On Windows, the st_mode member now identifies special files as
S_IFCHR , S_IFIFO or S_IFBLK as appropriate.
Two module-level constants are defined for the f_flag attribute’s bit-flags: if ST_RDONLY is
set, the filesystem is mounted read-only, and if ST_NOSUID is set, the semantics of
setuid/setgid bits are disabled or not supported.
Additional module-level constants are defined for GNU/glibc based systems. These are
ST_NODEV (disallow access to device special files), ST_NOEXEC (disallow program
execution), ST_SYNCHRONOUS (writes are synced at once), ST_MANDLOCK (allow
mandatory locks on an FS), ST_WRITE (write on file/directory/symlink), ST_APPEND
(append-only file), ST_IMMUTABLE (immutable file), ST_NOATIME (do not update access
times), ST_NODIRATIME (do not update directory access times), ST_RELATIME (update
atime relative to mtime/ctime).
Availability: Unix.
Changed in version 3.2: The ST_RDONLY and ST_NOSUID constants were added.
New in version 3.3: Added support for specifying path as an open file descriptor.
os. supports_dir_fd
A set object indicating which functions in the os module accept an open file descriptor for
their dir_fd parameter. Different platforms provide different features, and the underlying
functionality Python uses to implement the dir_fd parameter is not available on all platforms
46/77
Python supports. For consistency’s sake, functions that may support dir_fd always allow
specifying the parameter, but will throw an exception if the functionality is used when it’s
not locally available. (Specifying None for dir_fd is always supported on all platforms.)
To check whether a particular function accepts an open file descriptor for its dir_fd
parameter, use the in operator on supports_dir_fd . As an example, this expression
evaluates to True if os.stat() accepts open file descriptors for dir_fd on the local platform:
os.stat in os.supports_dir_fd
Currently dir_fd parameters only work on Unix platforms; none of them work on Windows.
os. supports_effective_ids
A set object indicating whether os.access() permits specifying True for its effective_ids
parameter on the local platform. (Specifying False for effective_ids is always supported on
all platforms.) If the local platform supports it, the collection will contain os.access();
otherwise it will be empty.
os.access in os.supports_effective_ids
Currently effective_ids is only supported on Unix platforms; it does not work on Windows.
os. supports_fd
A set object indicating which functions in the os module permit specifying their path
parameter as an open file descriptor on the local platform. Different platforms provide
different features, and the underlying functionality Python uses to accept open file
descriptors as path arguments is not available on all platforms Python supports.
To determine whether a particular function permits specifying an open file descriptor for its
path parameter, use the in operator on supports_fd . As an example, this expression
evaluates to True if os.chdir() accepts open file descriptors for path on your local platform:
os.chdir in os.supports_fd
os. supports_follow_symlinks
A set object indicating which functions in the os module accept False for their
follow_symlinks parameter on the local platform. Different platforms provide different
47/77
features, and the underlying functionality Python uses to implement follow_symlinks is not
available on all platforms Python supports. For consistency’s sake, functions that may
support follow_symlinks always allow specifying the parameter, but will throw an exception if
the functionality is used when it’s not locally available. (Specifying True for follow_symlinks
is always supported on all platforms.)
To check whether a particular function accepts False for its follow_symlinks parameter, use
the in operator on supports_follow_symlinks . As an example, this expression evaluates to
True if you may specify follow_symlinks=False when calling os.stat() on the local platform:
os.stat in os.supports_follow_symlinks
On Windows, a symlink represents either a file or a directory, and does not morph to the
target dynamically. If the target is present, the type of the symlink will be created to match.
Otherwise, the symlink will be created as a directory if target_is_directory is True or a file
symlink (the default) otherwise. On non-Windows platforms, target_is_directory is ignored.
Note
On newer versions of Windows 10, unprivileged accounts can create symlinks if Developer
Mode is enabled. When Developer Mode is not available/enabled, the
SeCreateSymbolicLinkPrivilege privilege is required, or the process must be run as an
administrator.
Changed in version 3.2: Added support for Windows 6.0 (Vista) symbolic links.
New in version 3.3: Added the dir_fd argument, and now allow target_is_directory on non-
Windows platforms.
Changed in version 3.6: Accepts a path-like object for src and dst.
Changed in version 3.8: Added support for unelevated symlinks on Windows with Developer
Mode.
48/77
os. sync ()
Force write of everything to disk.
Availability: Unix.
utime() takes two optional parameters, times and ns. These specify the times set on path and
are used as follows:
If times is not None , it must be a 2-tuple of the form (atime, mtime) where each
member is an int or float expressing seconds.
49/77
If times is None and ns is unspecified, this is equivalent to specifying ns=(atime_ns,
mtime_ns) where both times are the current time.
Note that the exact times you set here may not be returned by a subsequent stat() call,
depending on the resolution with which your operating system records access and
modification times; see stat(). The best way to preserve exact times is to use the st_atime_ns
and st_mtime_ns fields from the os.stat() result object with the ns parameter to utime.
This function can support specifying a file descriptor, paths relative to directory descriptors
and not following symlinks.
New in version 3.3: Added support for specifying path as an open file descriptor, and the
dir_fd, follow_symlinks, and ns parameters.
dirpath is a string, the path to the directory. dirnames is a list of the names of the
subdirectories in dirpath (excluding '.' and '..' ). filenames is a list of the names of the non-
directory files in dirpath. Note that the names in the lists contain no path components. To
get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath,
name) .
If optional argument topdown is True or not specified, the triple for a directory is generated
before the triples for any of its subdirectories (directories are generated top-down). If
topdown is False , the triple for a directory is generated after the triples for all of its
subdirectories (directories are generated bottom-up). No matter the value of topdown, the
list of subdirectories is retrieved before the tuples for the directory and its subdirectories
are generated.
When topdown is True , the caller can modify the dirnames list in-place (perhaps using del or
slice assignment), and walk() will only recurse into the subdirectories whose names remain
in dirnames; this can be used to prune the search, impose a specific order of visiting, or even
to inform walk() about directories the caller creates or renames before it resumes walk()
50/77
again. Modifying dirnames when topdown is False has no effect on the behavior of the walk,
because in bottom-up mode the directories in dirnames are generated before dirpath itself is
generated.
By default, errors from the scandir() call are ignored. If optional argument onerror is
specified, it should be a function; it will be called with one argument, an OSError instance. It
can report the error to continue with the walk, or raise the exception to abort the walk.
Note that the filename is available as the filename attribute of the exception object.
By default, walk() will not walk down into symbolic links that resolve to directories. Set
followlinks to True to visit directories pointed to by symlinks, on systems that support
them.
Note
Be aware that setting followlinks to True can lead to infinite recursion if a link points to a
parent directory of itself. walk() does not keep track of the directories it visited already.
Note
If you pass a relative pathname, don’t change the current working directory between
resumptions of walk(). walk() never changes the current directory, and assumes that its
caller doesn’t either.
This example displays the number of bytes taken by non-directory files in each directory
under the starting directory, except that it doesn’t look under any CVS subdirectory:
import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
print(root, "consumes", end=" ")
print(sum(getsize(join(root, name)) for name in files), end=" ")
print("bytes in", len(files), "non-directory files")
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
In the next example (simple implementation of shutil.rmtree()), walking the tree bottom-up is
essential, rmdir() doesn’t allow deleting a directory before the directory is empty:
51/77
# Delete everything reachable from the directory named in "top",
# assuming there are no symbolic links.
# CAUTION: This is dangerous! For example, if top == '/', it
# could delete all your disk files.
import os
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
Changed in version 3.5: This function now calls os.scandir() instead of os.listdir(), making it
faster by reducing the number of calls to os.stat().
dirpath, dirnames and filenames are identical to walk() output, and dirfd is a file descriptor
referring to the directory dirpath.
This function always supports paths relative to directory descriptors and not following
symlinks. Note however that, unlike other functions, the fwalk() default value for
follow_symlinks is False .
Note
Since fwalk() yields file descriptors, those are only valid until the next iteration step, so you
should duplicate them (e.g. with dup()) if you want to keep them longer.
This example displays the number of bytes taken by non-directory files in each directory
under the starting directory, except that it doesn’t look under any CVS subdirectory:
import os
for root, dirs, files, rootfd in os.fwalk('python/Lib/email'):
print(root, "consumes", end="")
print(sum([os.stat(name, dir_fd=rootfd).st_size for name in files]),
end="")
print("bytes in", len(files), "non-directory files")
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
In the next example, walking the tree bottom-up is essential: rmdir() doesn’t allow deleting a
directory before the directory is empty:
52/77
# Delete everything reachable from the directory named in "top",
# assuming there are no symbolic links.
# CAUTION: This is dangerous! For example, if top == '/', it
# could delete all your disk files.
import os
for root, dirs, files, rootfd in os.fwalk(top, topdown=False):
for name in files:
os.unlink(name, dir_fd=rootfd)
for name in dirs:
os.rmdir(name, dir_fd=rootfd)
Availability: Unix.
The name supplied in name is used as a filename and will be displayed as the target of the
corresponding symbolic link in the directory /proc/self/fd/ . The displayed name is always
prefixed with memfd: and serves only for debugging purposes. Names do not affect the
behavior of the file descriptor, and as such multiple files can have the same name without
any side effects.
os. MFD_CLOEXEC
os. MFD_ALLOW_SEALING
os. MFD_HUGETLB
os. MFD_HUGE_SHIFT
os. MFD_HUGE_MASK
os. MFD_HUGE_64KB
os. MFD_HUGE_512KB
os. MFD_HUGE_1MB
os. MFD_HUGE_2MB
os. MFD_HUGE_8MB
os. MFD_HUGE_16MB
os. MFD_HUGE_32MB
os. MFD_HUGE_256MB
53/77
os. MFD_HUGE_512MB
os. MFD_HUGE_1GB
os. MFD_HUGE_2GB
os. MFD_HUGE_16GB
These flags can be passed to memfd_create().
Availability: Linux 3.17 or newer with glibc 2.27 or newer. The MFD_HUGE* flags are only
available since Linux 4.14.
This function can support specifying a file descriptor and not following symlinks.
Changed in version 3.6: Accepts a path-like object for path and attribute.
This function can support specifying a file descriptor and not following symlinks.
This function can support specifying a file descriptor and not following symlinks.
54/77
Raises an auditing event os.removexattr with arguments path , attribute .
Changed in version 3.6: Accepts a path-like object for path and attribute.
This function can support specifying a file descriptor and not following symlinks.
Note
A bug in Linux kernel versions less than 2.6.39 caused the flags argument to be ignored on
some filesystems.
Raises an auditing event os.setxattr with arguments path , attribute , value , flags .
Changed in version 3.6: Accepts a path-like object for path and attribute.
os. XATTR_SIZE_MAX
The maximum size the value of an extended attribute can be. Currently, this is 64 KiB on
Linux.
os. XATTR_CREATE
This is a possible value for the flags argument in setxattr(). It indicates the operation must
create an attribute.
os. XATTR_REPLACE
This is a possible value for the flags argument in setxattr(). It indicates the operation must
replace an existing attribute.
Process Management
These functions may be used to create and manage processes.
The various exec* functions take a list of arguments for the new program loaded into the
process. In each case, the first of these arguments is passed to the new program as its own
name rather than as an argument a user may have typed on a command line. For the C
programmer, this is the argv[0] passed to a program’s main() . For example,
os.execv('/bin/echo', ['foo', 'bar']) will only print bar on standard output; foo will seem to
be ignored.
55/77
os. abort ()
Generate a SIGABRT signal to the current process. On Unix, the default behavior is to
produce a core dump; on Windows, the process immediately returns an exit code of 3 . Be
aware that calling this function will not call the Python signal handler registered for
SIGABRT with signal.signal().
This search path is used when resolving dependencies for imported extension modules (the
module itself is resolved through sys.path), and also by ctypes.
Remove the directory by calling close() on the returned object or using it in a with
statement.
See the Microsoft documentation for more information about how DLLs are loaded.
Availability: Windows.
New in version 3.8: Previous versions of CPython would resolve DLLs using the default
behavior for the current process. This led to inconsistencies, such as only sometimes
searching PATH or the current working directory, and OS functions such as
AddDllDirectory having no effect.
In 3.8, the two primary ways DLLs are loaded now explicitly override the process-wide
behavior to ensure consistency. See the porting notes for information on updating libraries.
The current process is replaced immediately. Open file objects and descriptors are not
flushed, so if there may be data buffered on these open files, you should flush them using
sys.stdout.flush() or os.fsync() before calling an exec* function.
56/77
The “l” and “v” variants of the exec* functions differ in how command-line arguments are
passed. The “l” variants are perhaps the easiest to work with if the number of parameters is
fixed when the code is written; the individual parameters simply become additional
parameters to the execl*() functions. The “v” variants are good when the number of
parameters is variable, with the arguments being passed in a list or tuple as the args
parameter. In either case, the arguments to the child process should start with the name of
the command being run, but this is not enforced.
The variants which include a “p” near the end ( execlp(), execlpe(), execvp(), and execvpe())
will use the PATH environment variable to locate the program file. When the environment
is being replaced (using one of the exec*e variants, discussed in the next paragraph), the
new environment is used as the source of the PATH variable. The other variants, execl() ,
execle() , execv() , and execve() , will not use the PATH variable to locate the executable; path
must contain an appropriate absolute or relative path.
For execle() , execlpe(), execve(), and execvpe() (note that these all end in “e”), the env
parameter must be a mapping which is used to define the environment variables for the
new process (these are used instead of the current process’ environment); the functions
execl() , execlp(), execv() , and execvp() all cause the new process to inherit the environment
of the current process.
For execve() on some platforms, path may also be specified as an open file descriptor. This
functionality may not be supported on your platform; you can check whether or not it is
available using os.supports_fd. If it is unavailable, using it will raise a NotImplementedError.
New in version 3.3: Added support for specifying path as an open file descriptor for execve().
Note
The standard way to exit is sys.exit(n) . _exit() should normally only be used in the child
process after a fork() .
The following exit codes are defined and can be used with _exit(), although they are not
required. These are typically used for system programs written in Python, such as a mail
server’s external command delivery program.
57/77
Note
Some of these may not be available on all Unix platforms, since there is some variation.
These constants are defined where they are defined by the underlying platform.
os. EX_OK
Exit code that means no error occurred.
Availability: Unix.
os. EX_USAGE
Exit code that means the command was used incorrectly, such as when the wrong number
of arguments are given.
Availability: Unix.
os. EX_DATAERR
Exit code that means the input data was incorrect.
Availability: Unix.
os. EX_NOINPUT
Exit code that means an input file did not exist or was not readable.
Availability: Unix.
os. EX_NOUSER
Exit code that means a specified user did not exist.
Availability: Unix.
os. EX_NOHOST
Exit code that means a specified host did not exist.
Availability: Unix.
os. EX_UNAVAILABLE
Exit code that means that a required service is unavailable.
Availability: Unix.
os. EX_SOFTWARE
Exit code that means an internal software error was detected.
Availability: Unix.
os. EX_OSERR
Exit code that means an operating system error was detected, such as the inability to fork or
58/77
create a pipe.
Availability: Unix.
os. EX_OSFILE
Exit code that means some system file did not exist, could not be opened, or had some
other kind of error.
Availability: Unix.
os. EX_CANTCREAT
Exit code that means a user specified output file could not be created.
Availability: Unix.
os. EX_IOERR
Exit code that means that an error occurred while doing I/O on some file.
Availability: Unix.
os. EX_TEMPFAIL
Exit code that means a temporary failure occurred. This indicates something that may not
really be an error, such as a network connection that couldn’t be made during a retryable
operation.
Availability: Unix.
os. EX_PROTOCOL
Exit code that means that a protocol exchange was illegal, invalid, or not understood.
Availability: Unix.
os. EX_NOPERM
Exit code that means that there were insufficient permissions to perform the operation (but
not intended for file system problems).
Availability: Unix.
os. EX_CONFIG
Exit code that means that some kind of configuration error occurred.
Availability: Unix.
os. EX_NOTFOUND
Exit code that means something like “an entry was not found”.
Availability: Unix.
59/77
os. fork ()
Fork a child process. Return 0 in the child and the child’s process id in the parent. If an
error occurs OSError is raised.
Note that some platforms including FreeBSD <= 6.3 and Cygwin have known issues when
using fork() from a thread.
Warning
See ssl for applications that use the SSL module with fork().
Availability: Unix.
os. forkpty ()
Fork a child process, using a new pseudo-terminal as the child’s controlling terminal. Return
a pair of (pid, fd) , where pid is 0 in the child, the new child’s process id in the parent, and
fd is the file descriptor of the master end of the pseudo-terminal. For a more portable
approach, use the pty module. If an error occurs OSError is raised.
60/77
New in version 3.2: Windows support.
Availability: Unix.
Availability: Unix.
Availability: Unix.
The close method returns None if the subprocess exited successfully, or the subprocess’s
return code if there was an error. On POSIX systems, if the return code is positive it
represents the return value of the process left-shifted by one byte. If the return code is
negative, the process was terminated by the signal given by the negated value of the return
code. (For example, the return value might be - signal.SIGKILL if the subprocess was killed.)
On Windows systems, the return value contains the signed integer return code from the
child process.
This is implemented using subprocess.Popen; see that class’s documentation for more
powerful ways to manage and communicate with subprocesses.
The positional-only arguments path, args, and env are similar to execve().
61/77
The path parameter is the path to the executable file.The path should contain a
directory.Use posix_spawnp() to pass an executable file without directory.
The file_actions argument may be a sequence of tuples describing actions to take on specific
file descriptors in the child process between the C library implementation’s fork() and
exec() steps. The first item in each tuple must be one of the three type indicator listed
below describing the remaining tuple elements:
os. POSIX_SPAWN_OPEN
( os.POSIX_SPAWN_OPEN , fd, path, flags, mode)
os. POSIX_SPAWN_CLOSE
( os.POSIX_SPAWN_CLOSE , fd)
Performs os.close(fd) .
os. POSIX_SPAWN_DUP2
( os.POSIX_SPAWN_DUP2 , fd, new_fd)
The setpgroup argument will set the process group of the child to the value specified. If the
value specified is 0, the child’s process group ID will be made the same as its process ID. If
the value of setpgroup is not set, the child will inherit the parent’s process group ID. This
argument corresponds to the C library POSIX_SPAWN_SETPGROUP flag.
If the resetids argument is True it will reset the effective UID and GID of the child to the real
UID and GID of the parent process. If the argument is False , then the child retains the
effective UID and GID of the parent. In either case, if the set-user-ID and set-group-ID
permission bits are enabled on the executable file, their effect will override the setting of
the effective UID and GID. This argument corresponds to the C library
POSIX_SPAWN_RESETIDS flag.
If the setsid argument is True , it will create a new session ID for posix_spawn. setsid requires
POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP flag. Otherwise, NotImplementedError is
raised.
62/77
The setsigmask argument will set the signal mask to the signal set specified. If the parameter
is not used, then the child inherits the parent’s signal mask. This argument corresponds to
the C library POSIX_SPAWN_SETSIGMASK flag.
The sigdef argument will reset the disposition of all signals in the set specified. This
argument corresponds to the C library POSIX_SPAWN_SETSIGDEF flag.
The scheduler argument must be a tuple containing the (optional) scheduler policy and an
instance of sched_param with the scheduler parameters. A value of None in the place of the
scheduler policy indicates that is not being provided. This argument is a combination of the
C library POSIX_SPAWN_SETSCHEDPARAM and POSIX_SPAWN_SETSCHEDULER flags.
Availability: Unix.
Similar to posix_spawn() except that the system searches for the executable file in the list of
directories specified by the PATH environment variable (in the same way as for
execvp(3) ).
after_in_parent is a function called from the parent process after forking a child
process.
63/77
These calls are only made if control is expected to return to the Python interpreter. A typical
subprocess launch will not trigger them as the child is not going to re-enter the interpreter.
Functions registered for execution before forking are called in reverse registration order.
Functions registered for execution after forking (either in the parent or in the child) are
called in registration order.
Note that fork() calls made by third-party C code may not call those functions, unless it
explicitly calls PyOS_BeforeFork(), PyOS_AfterFork_Parent() and PyOS_AfterFork_Child().
Availability: Unix.
(Note that the subprocess module provides more powerful facilities for spawning new
processes and retrieving their results; using that module is preferable to using these
functions. Check especially the Replacing Older Functions with the subprocess Module
section.)
If mode is P_NOWAIT, this function returns the process id of the new process; if mode is
P_WAIT, returns the process’s exit code if it exits normally, or -signal , where signal is the
signal that killed the process. On Windows, the process id will actually be the process
handle, so can be used with the waitpid() function.
Note on VxWorks, this function doesn’t return -signal when the new process is killed.
Instead it raises OSError exception.
The “l” and “v” variants of the spawn* functions differ in how command-line arguments are
passed. The “l” variants are perhaps the easiest to work with if the number of parameters is
fixed when the code is written; the individual parameters simply become additional
parameters to the spawnl*() functions. The “v” variants are good when the number of
64/77
parameters is variable, with the arguments being passed in a list or tuple as the args
parameter. In either case, the arguments to the child process must start with the name of
the command being run.
The variants which include a second “p” near the end ( spawnlp(), spawnlpe(), spawnvp(), and
spawnvpe()) will use the PATH environment variable to locate the program file. When the
environment is being replaced (using one of the spawn*e variants, discussed in the next
paragraph), the new environment is used as the source of the PATH variable. The other
variants, spawnl() , spawnle() , spawnv(), and spawnve(), will not use the PATH variable to
locate the executable; path must contain an appropriate absolute or relative path.
For spawnle() , spawnlpe(), spawnve(), and spawnvpe() (note that these all end in “e”), the env
parameter must be a mapping which is used to define the environment variables for the
new process (they are used instead of the current process’ environment); the functions
spawnl() , spawnlp(), spawnv() , and spawnvp() all cause the new process to inherit the
environment of the current process. Note that keys and values in the env dictionary must be
strings; invalid keys or values will cause the function to fail, with a return value of 127 .
import os
os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
Raises an auditing event os.spawn with arguments mode , path , args , env .
Availability: Unix, Windows. spawnlp(), spawnlpe(), spawnvp() and spawnvpe() are not
available on Windows. spawnle() and spawnve() are not thread-safe on Windows; we advise
you to use the subprocess module instead.
os. P_NOWAIT
os. P_NOWAITO
Possible values for the mode parameter to the spawn* family of functions. If either of these
values is given, the spawn*() functions will return as soon as the new process has been
created, with the process id as the return value.
os. P_WAIT
Possible value for the mode parameter to the spawn* family of functions. If this is given as
mode, the spawn*() functions will not return until the new process has run to completion
65/77
and will return the exit code of the process the run is successful, or -signal if a signal kills
the process.
os. P_DETACH
os. P_OVERLAY
Possible values for the mode parameter to the spawn* family of functions. These are less
portable than those listed above. P_DETACH is similar to P_NOWAIT, but the new process is
detached from the console of the calling process. If P_OVERLAY is used, the current process
will be replaced; the spawn* function will not return.
Availability: Windows.
When operation is not specified or 'open' , this acts like double-clicking the file in Windows
Explorer, or giving the file name as an argument to the start command from the interactive
command shell: the file is opened with whatever application (if any) its extension is
associated.
When another operation is given, it must be a “command verb” that specifies what should be
done with the file. Common verbs documented by Microsoft are 'print' and 'edit' (to be
used on files) as well as 'explore' and 'find' (to be used on directories).
To reduce interpreter startup overhead, the Win32 ShellExecute() function is not resolved
until this function is first called. If the function cannot be resolved, NotImplementedError will
be raised.
Availability: Windows.
On Unix, the return value is the exit status of the process encoded in the format specified
for wait(). Note that POSIX does not specify the meaning of the return value of the C
system() function, so the return value of the Python function is system-dependent.
On Windows, the return value is that returned by the system shell after running command.
The shell is given by the Windows environment variable COMSPEC : it is usually cmd.exe,
which returns the exit status of the command run; on systems using a non-native shell,
consult your shell documentation.
The subprocess module provides more powerful facilities for spawning new processes and
retrieving their results; using that module is preferable to using this function. See the
Replacing Older Functions with the subprocess Module section in the subprocess
documentation for some helpful recipes.
os. times ()
Returns the current global process times. The return value is an object with five attributes:
For backwards compatibility, this object also behaves like a five-tuple containing user ,
system, children_user , children_system , and elapsed in that order.
See the Unix manual page times(2) and times(3) manual page on Unix or the
GetProcessTimes MSDN on Windows. On Windows, only user and system are known; the
other attributes are zero.
Changed in version 3.3: Return type changed from a tuple to a tuple-like object with named
attributes.
os. wait ()
67/77
Wait for completion of a child process, and return a tuple containing its pid and exit status
indication: a 16-bit number, whose low byte is the signal number that killed the process, and
whose high byte is the exit status (if the signal number is zero); the high bit of the low byte is
set if a core file was produced.
Availability: Unix.
Availability: Unix.
os. P_PID
os. P_PGID
os. P_ALL
These are the possible values for idtype in waitid(). They affect how id is interpreted.
Availability: Unix.
os. WEXITED
os. WSTOPPED
os. WNOWAIT
Flags that can be used in options in waitid() that specify what child signal to wait for.
Availability: Unix.
os. CLD_EXITED
os. CLD_DUMPED
os. CLD_TRAPPED
os. CLD_CONTINUED
These are the possible values for si_code in the result returned by waitid().
Availability: Unix.
68/77
os. waitpid (pid, options)
The details of this function differ on Unix and Windows.
On Unix: Wait for completion of a child process given by process id pid, and return a tuple
containing its process id and exit status indication (encoded as for wait()). The semantics of
the call are affected by the value of the integer options, which should be 0 for normal
operation.
If pid is greater than 0 , waitpid() requests status information for that specific process. If pid
is 0 , the request is for the status of any child in the process group of the current process. If
pid is -1 , the request pertains to any child of the current process. If pid is less than -1 ,
status is requested for any process in the process group -pid (the absolute value of pid).
An OSError is raised with the value of errno when the syscall returns -1.
On Windows: Wait for completion of a process given by process handle pid, and return a
tuple containing pid, and its exit status shifted left by 8 bits (shifting makes cross-platform
use of the function easier). A pid less than or equal to 0 has no special meaning on
Windows, and raises an exception. The value of integer options has no effect. pid can refer
to any process whose id is known, not necessarily a child process. The spawn* functions
called with P_NOWAIT return suitable process handles.
Changed in version 3.5: If the system call is interrupted and the signal handler does not
raise an exception, the function now retries the system call instead of raising an
InterruptedError exception (see PEP 475 for the rationale).
Availability: Unix.
Availability: Unix.
os. WNOHANG
69/77
The option for waitpid() to return immediately if no child process status is available
immediately. The function returns (0, 0) in this case.
Availability: Unix.
os. WCONTINUED
This option causes child processes to be reported if they have been continued from a job
control stop since their status was last reported.
os. WUNTRACED
This option causes child processes to be reported if they have been stopped but their
current state has not been reported since they were stopped.
Availability: Unix.
The following functions take a process status code as returned by system(), wait(), or
waitpid() as a parameter. They may be used to determine the disposition of a process.
Availability: Unix.
Availability: Unix.
WIFSTOPPED() only returns True if the waitpid() call was done using WUNTRACED option or
when the process is being traced (see ptrace(2)).
Availability: Unix.
70/77
Availability: Unix.
Availability: Unix.
Availability: Unix.
Availability: Unix.
Availability: Unix.
The following scheduling policies are exposed if they are supported by the operating
system.
os. SCHED_OTHER
The default scheduling policy.
os. SCHED_BATCH
Scheduling policy for CPU-intensive processes that tries to preserve interactivity on the rest
of the computer.
71/77
os. SCHED_IDLE
Scheduling policy for extremely low priority background tasks.
os. SCHED_SPORADIC
Scheduling policy for sporadic server programs.
os. SCHED_FIFO
A First In First Out scheduling policy.
os. SCHED_RR
A round-robin scheduling policy.
os. SCHED_RESET_ON_FORK
This flag can be OR’ed with any other scheduling policy. When a process with this flag set
forks, its child’s scheduling policy and priority are reset to the default.
sched_priority
The scheduling priority for a scheduling policy.
os. sched_yield ()
Voluntarily relinquish the CPU.
If name is a string and is not known, ValueError is raised. If a specific value for name is not
supported by the host system, even if it is included in confstr_names , an OSError is raised
with errno.EINVAL for the error number.
Availability: Unix.
os. confstr_names
Dictionary mapping names accepted by confstr() to the integer values defined for those
names by the host operating system. This can be used to determine the set of names
known to the system.
Availability: Unix.
os. cpu_count ()
73/77
Return the number of CPUs in the system. Returns None if undetermined.
This number is not equivalent to the number of CPUs the current process can use. The
number of usable CPUs can be obtained with len(os.sched_getaffinity(0))
os. getloadavg ()
Return the number of processes in the system run queue averaged over the last 1, 5, and 15
minutes or raises OSError if the load average was unobtainable.
Availability: Unix.
Availability: Unix.
os. sysconf_names
Dictionary mapping names accepted by sysconf() to the integer values defined for those
names by the host operating system. This can be used to determine the set of names
known to the system.
Availability: Unix.
The following data values are used to support path manipulation operations. These are
defined for all platforms.
os. curdir
The constant string used by the operating system to refer to the current directory. This is
'.' for Windows and POSIX. Also available via os.path.
os. pardir
The constant string used by the operating system to refer to the parent directory. This is
'..' for Windows and POSIX. Also available via os.path.
os. sep
The character used by the operating system to separate pathname components. This is '/'
for POSIX and '\\' for Windows. Note that knowing this is not sufficient to be able to parse
or concatenate pathnames — use os.path.split() and os.path.join() — but it is occasionally
74/77
useful. Also available via os.path.
os. altsep
An alternative character used by the operating system to separate pathname components,
or None if only one separator character exists. This is set to '/' on Windows systems
where sep is a backslash. Also available via os.path.
os. extsep
The character which separates the base filename from the extension; for example, the '.'
in os.py . Also available via os.path.
os. pathsep
The character conventionally used by the operating system to separate search path
components (as in PATH ), such as ':' for POSIX or ';' for Windows. Also available via
os.path.
os. defpath
The default search path used by exec*p* and spawn*p* if the environment doesn’t have a
'PATH' key. Also available via os.path.
os. linesep
The string used to separate (or, rather, terminate) lines on the current platform. This may be
a single character, such as '\n' for POSIX, or multiple characters, for example, '\r\n' for
Windows. Do not use os.linesep as a line terminator when writing files opened in text mode
(the default); use a single '\n' instead, on all platforms.
os. devnull
The file path of the null device. For example: '/dev/null' for POSIX, 'nul' for Windows. Also
available via os.path.
os. RTLD_LAZY
os. RTLD_NOW
os. RTLD_GLOBAL
os. RTLD_LOCAL
os. RTLD_NODELETE
os. RTLD_NOLOAD
os. RTLD_DEEPBIND
Flags for use with the setdlopenflags() and getdlopenflags() functions. See the Unix manual
page dlopen(3) for what the different flags mean.
Random numbers
os. getrandom (size, flags=0)
75/77
Get up to size random bytes. The function can return less bytes than requested.
These bytes can be used to seed user-space random number generators or for
cryptographic purposes.
getrandom() relies on entropy gathered from device drivers and other sources of
environmental noise. Unnecessarily reading large quantities of data will have a negative
impact on other users of the /dev/random and /dev/urandom devices.
The flags argument is a bit mask that can contain zero or more of the following values ORed
together: os.GRND_RANDOM and GRND_NONBLOCK.
This function returns random bytes from an OS-specific randomness source. The returned
data should be unpredictable enough for cryptographic applications, though its exact quality
depends on the OS implementation.
On Linux, if the getrandom() syscall is available, it is used in blocking mode: block until the
system urandom entropy pool is initialized (128 bits of entropy are collected by the kernel).
See the PEP 524 for the rationale. On Linux, the getrandom() function can be used to get
random bytes in non-blocking mode (using the GRND_NONBLOCK flag) or to poll until the
system urandom entropy pool is initialized.
On a Unix-like system, random bytes are read from the /dev/urandom device. If the
/dev/urandom device is not available or not readable, the NotImplementedError exception is
raised.
See also
The secrets module provides higher level functions. For an easy-to-use interface to the
random number generator provided by your platform, please see random.SystemRandom.
Changed in version 3.6.0: On Linux, getrandom() is now used in blocking mode to increase
the security.
76/77
Changed in version 3.5.2: On Linux, if the getrandom() syscall blocks (the urandom entropy
pool is not initialized yet), fall back on reading /dev/urandom .
Changed in version 3.5: On Linux 3.17 and newer, the getrandom() syscall is now used
when available. On OpenBSD 5.6 and newer, the C getentropy() function is now used.
These functions avoid the usage of an internal file descriptor.
os. GRND_NONBLOCK
By default, when reading from /dev/random , getrandom() blocks if no random bytes are
available, and when reading from /dev/urandom , it blocks if the entropy pool has not yet
been initialized.
If the GRND_NONBLOCK flag is set, then getrandom() does not block in these cases, but
instead immediately raises BlockingIOError.
os. GRND_RANDOM
If this bit is set, then random bytes are drawn from the /dev/random pool instead of the
/dev/urandom pool.
77/77