python / 3.7.2rc1 / all / library-os.html

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 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.

  • On VxWorks, 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.

exception os. error

An alias for the built-in OSError exception.

os. name

The name of the operating system dependent module imported. The following names have currently been registered: 'posix', 'nt', 'java'.

See also

sys.platform has a finer granularity. os.uname() gives system-dependent version information.

The platform module 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 file system encoding to perform this conversion (see sys.getfilesystemencoding()).

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.

On Unix, keys and values use sys.getfilesystemencoding() and 'surrogateescape' error handler. Use environb if you would like to use a different encoding.

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).

environb is only available if supports_bytes_environ is True.

New in version 3.2.

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 with 'surrogateescape' error handler, or 'strict' on Windows; return bytes unchanged.

fsdecode() is the reverse function.

New in version 3.2.

Changed in version 3.6: Support added to accept objects implementing the os.PathLike interface.

os. fsdecode ( filename )

Decode the path-like filename from the filesystem encoding with 'surrogateescape' error handler, or 'strict' on Windows; return str unchanged.

fsencode() is the reverse function.

New in version 3.2.

Changed in version 3.6: Support added to accept objects implementing the os.PathLike interface.

os. fspath ( path )

Return the file system representation of the path.

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.

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.

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.

os. getenv ( key, default=None )

Return the value of the environment variable key if it exists, or default if it doesn’t. key, default and the result are str.

On Unix, keys and values are decoded with sys.getfilesystemencoding() and 'surrogateescape' error handler. Use os.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 if it exists, or default if it doesn’t. key, default and the result are bytes.

getenvb() is only available if supports_bytes_environ is True.

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, environ is 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 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 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, 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, or PRIO_USER, and who is interpreted relative to which (a process identifier for PRIO_PROCESS, process group identifier for PRIO_PGRP, and a user ID for PRIO_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() and setpriority() 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() or fork() and execv().

Availability: most flavors of Unix, Windows.

Note

On some platforms, including FreeBSD and Mac OS X, setting environ may cause memory leaks. Refer to the system documentation for putenv.

When putenv() is supported, assignments to items in os.environ are automatically translated into corresponding calls to putenv(); however, calls to putenv() don’t update os.environ, so it is actually preferable to assign to items of os.environ.

Raises an auditing event os.putenv with arguments key, value.

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 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.

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, or PRIO_USER, and who is interpreted relative to which (a process identifier for PRIO_PROCESS, process group identifier for PRIO_PGRP, and a user ID for PRIO_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() returns NULL when given an unknown error number, ValueError is raised.

os. supports_bytes_environ

True if the native OS type of the environment is bytes (eg. False on 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 name

  • nodename - name of machine on network (implementation-defined)

  • release - operating system release

  • version - operating system version

  • machine - hardware identifier

For backwards compatibility, this object is also iterable, behaving like a five-tuple containing sysname, nodename, release, version, and machine in that order.

Some systems truncate nodename to 8 characters or to the leading component; a better way to get the hostname is socket.gethostname() or even socket.gethostbyaddr(socket.gethostname()).

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() or fork() and execv().

When unsetenv() is supported, deletion of items in os.environ is automatically translated into a corresponding call to unsetenv(); however, calls to unsetenv() don’t update os.environ, so it is actually preferable to delete items of os.environ.

Raises an auditing event os.unsetenv with argument key.

Availability: most flavors of Unix.

File Object Creation

These functions create new file objects. (See also open() for opening file descriptors.)

os. fdopen ( fd, *args, **kwargs )

Return an open file object connected to the file descriptor fd. This is an alias of the open() built-in function and accepts the same arguments. The only difference is that the first argument of fdopen() must always be an integer.

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.

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 close a “file object” returned by the built-in function open() or by popen() or fdopen(), use its close() method.

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 OSError is raised with errno set to errno.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.

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, None was always returned.

os. fchmod ( fd, mode )

Change the mode of the file given by fd to the numeric mode. See the docs for chmod() for possible values of mode. As of Python 3.3, this is equivalent to os.chmod(fd, mode).

Raises an auditing event os.chmod with arguments path, mode, dir_fd.

Availability: Unix.

os. fchown ( fd, uid, gid )

Change the owner and group id of the file given by fd to the numeric uid and gid. To leave one of the ids unchanged, set it to -1. See chown(). As of Python 3.3, this is equivalent to os.chown(fd, uid, gid).

Raises an auditing event os.chown with arguments path, uid, gid, dir_fd.

Availability: Unix.

os. fdatasync ( fd )

Force write of file with filedescriptor fd to disk. Does not force update of metadata.

Availability: Unix.

Note

This function is not available on MacOS.

os. fpathconf ( fd, name )

Return system configuration information relevant to an open file. name specifies the configuration value to retrieve; it may be a string which is the name of a defined system value; these names are specified in a number of standards (POSIX.1, Unix 95, Unix 98, and 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.

As of Python 3.3, this is equivalent to os.pathconf(fd, name).

Availability: Unix.

os. fstat ( fd )

Get the status of the file descriptor fd. Return a stat_result object.

As of Python 3.3, this is equivalent to os.stat(fd).

See also

The stat() function.

os. fstatvfs ( fd )

Return information about the filesystem containing the file associated with file descriptor fd, like statvfs(). As of Python 3.3, this is equivalent to os.statvfs(fd).

Availability: Unix.

os. fsync ( fd )

Force write of file with filedescriptor fd to disk. On Unix, this calls the native fsync() function; on Windows, the MS _commit() function.

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, Windows.

os. ftruncate ( fd, length )

Truncate the file corresponding to file descriptor fd, so that it is at most length bytes in size. As of Python 3.3, this is equivalent to os.truncate(fd, length).

Raises an auditing event os.truncate with arguments fd, length.

Availability: Unix, Windows.

Changed in version 3.5: Added support for Windows

os. get_blocking ( fd )

Get the blocking mode of the file descriptor: False if the O_NONBLOCK flag is set, True if the flag is cleared.

See also set_blocking() and socket.socket.setblocking().

Availability: Unix.

New in version 3.5.

os. isatty ( fd )

Return True if the file descriptor fd is open and connected to a tty(-like) device, else False.

os. lockf ( fd, cmd, len )

Apply, test or remove a POSIX lock on an open file descriptor. fd is an open file descriptor. cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. len specifies the section of the file to lock.

Raises an auditing event os.lockf with arguments fd, cmd, len.

Availability: Unix.

New in version 3.3.

os. F_LOCK
os. F_TLOCK
os. F_ULOCK
os. F_TEST

Flags that specify what action lockf() will take.

Availability: Unix.

New in version 3.3.

os. lseek ( fd, pos, how )

Set the current position of file descriptor fd to position pos, modified by how: SEEK_SET or 0 to set the position relative to the beginning of the file; SEEK_CUR or 1 to set it relative to the current position; SEEK_END or 2 to set it relative to the end of the file. Return the new cursor position in bytes, starting from the beginning.

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.

os. open ( path, flags, mode=0o777, *, dir_fd=None )

Open the file path and set various flags according to flags and possibly its mode according to mode. When computing mode, the current umask value is first masked out. Return the file descriptor for the newly opened file. The new file descriptor is non-inheritable.

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.

Raises an auditing event open with arguments path, mode, flags.

Changed in version 3.4: The new file descriptor is now non-inheritable.

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().

New in version 3.3: The dir_fd argument.

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).

Changed in version 3.6: Accepts a path-like object.

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.

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.

Availability: some flavors of Unix.

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.

Availability: Unix, Windows.

Changed in version 3.4: The new file descriptors are now non-inheritable.

os. pipe2 ( flags )

Create a pipe with flags set atomically. flags can be constructed by ORing together one or more of these values: O_NONBLOCK, O_CLOEXEC. Return a pair of file descriptors (r, w) usable for reading and writing, respectively.

Availability: some flavors of Unix.

New in version 3.3.

os. posix_fallocate ( fd, offset, len )

Ensures that enough disk space is allocated for the file specified by fd starting from offset and continuing for len bytes.

Availability: Unix.

New in version 3.3.

os. posix_fadvise ( fd, offset, len, advice )

Announces an intention to access data in a specific pattern thus allowing the kernel to make optimizations. The advice applies to the region of the file specified by fd starting at offset and continuing for len bytes. advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or POSIX_FADV_DONTNEED.

Availability: Unix.

New in version 3.3.

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.

New in version 3.3.

os. pread ( fd, n, offset )

Read at most n bytes from file descriptor fd at a position of offset, leaving the file offset unchanged.

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.

New in version 3.3.

os. preadv ( fd, buffers, offset, flags=0 )

Read from a file descriptor fd at a position of offset into mutable bytes-like objects buffers, leaving the file offset unchanged. Transfer data into each buffer until it is full and then move on to the next buffer in the sequence to hold the rest of the data.

The flags argument contains a bitwise OR of zero or more of the following flags:

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.

Combine the functionality of os.readv() and os.pread().

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.

New in version 3.7.

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.

Availability: Linux 4.14 and newer.

New in version 3.7.

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: Linux 4.6 and newer.

New in version 3.7.

os. pwrite ( fd, str, offset )

Write the bytestring in str to file descriptor fd at position of offset, leaving the file offset unchanged.

Return the number of bytes actually written.

Availability: Unix.

New in version 3.3.

os. pwritev ( fd, buffers, offset, flags=0 )

Write the buffers contents to file descriptor fd at a offset offset, leaving the file offset unchanged. buffers must be a sequence of bytes-like objects. Buffers are processed in array order. Entire contents of the first buffer is written before proceeding to the second, and so on.

The flags argument contains a bitwise OR of zero or more of the following flags:

Return the total number of bytes actually written.

The operating system may set a limit (sysconf() value 'SC_IOV_MAX') on the number of buffers that can be used.

Combine the functionality of os.writev() and os.pwrite().

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.

New in version 3.7.

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.

Availability: Linux 4.7 and newer.

New in version 3.7.

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.

Availability: Linux 4.7 and newer.

New in version 3.7.

os. read ( fd, n )

Read at most n bytes from file descriptor fd.

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).

os. sendfile ( out, in, offset, count )
os. sendfile ( out, in, offset, count, [ headers, ] [ trailers, ] flags=0 )

Copy count bytes from file descriptor in to file descriptor out starting at offset. Return the number of bytes sent. When EOF is reached return 0.

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

For a higher-level wrapper of sendfile(), see socket.socket.sendfile().

New in version 3.3.

os. set_blocking ( fd, blocking )

Set the blocking mode of the specified file descriptor. Set the O_NONBLOCK flag if blocking is False, clear the flag otherwise.

See also get_blocking() and socket.socket.setblocking().

Availability: Unix.

New in version 3.5.

os. SF_NODISKIO
os. SF_MNOWAIT
os. SF_SYNC

Parameters to the sendfile() function, if the implementation supports them.

Availability: Unix.

New in version 3.3.

os. readv ( fd, buffers )

Read from a file descriptor fd into a number of mutable bytes-like objects buffers. Transfer data into each buffer until it is full and then move on to the next buffer in the sequence to hold the rest of the data.

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.

New in version 3.3.

os. tcgetpgrp ( fd )

Return the process group associated with the terminal given by fd (an open file descriptor as returned by os.open()).

Availability: Unix.

os. tcsetpgrp ( fd, pg )

Set the process group associated with the terminal given by fd (an open file descriptor as returned by os.open()) to pg.

Availability: Unix.

os. ttyname ( fd )

Return a string which specifies the terminal device associated with file descriptor fd. If fd is not associated with a terminal device, an exception is raised.

Availability: Unix.

os. write ( fd, str )

Write the bytestring in str to file descriptor fd.

Return the number of bytes actually written.

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).

os. writev ( fd, buffers )

Write the contents of buffers to file descriptor fd. buffers must be a sequence of bytes-like objects. Buffers are processed in array order. Entire contents of the first buffer is written before proceeding to the second, and so on.

Returns the total number of bytes actually written.

The operating system may set a limit (sysconf() value 'SC_IOV_MAX') on the number of buffers that can be used.

Availability: Unix.

New in version 3.3.

Querying the size of a terminal

New in version 3.3.

os. get_terminal_size ( fd=STDOUT_FILENO )

Return the size of the terminal window as (columns, lines), tuple of type terminal_size.

The optional argument fd (default STDOUT_FILENO, or standard output) specifies which file descriptor should be queried.

If the file descriptor is not connected to a terminal, an OSError is raised.

shutil.get_terminal_size() is the high-level function which should normally be used, os.get_terminal_size is the low-level implementation.

Availability: Unix, Windows.

class os. terminal_size

A subclass of tuple, holding (columns, lines) of the terminal window size.

columns

Width of the terminal window in characters.

lines

Height of the terminal window in characters.

Inheritance of File Descriptors

New in version 3.4.

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.

os. get_inheritable ( fd )

Get the “inheritable” flag of the specified file descriptor (a boolean).

os. set_inheritable ( fd, inheritable )

Set the “inheritable” flag of the specified file descriptor.

os. get_handle_inheritable ( handle )

Get the “inheritable” flag of the specified handle (a boolean).

Availability: Windows.

os. set_handle_inheritable ( handle, inheritable )

Set the “inheritable” flag of the specified handle.

Availability: Windows.

Files and Directories

On some Unix platforms, many of these functions support one or more of these features:

  • 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.

  • paths relative to directory descriptors: If dir_fd is not None, it should be a file descriptor referring to a directory, and the path to operate on should be relative; path will then be relative to that directory. If the path is absolute, dir_fd is ignored. (For POSIX systems, Python will call the variant of the function with an at suffix and possibly prefixed with f (e.g. call faccessat instead of access).

    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.

os. access ( path, mode, *, dir_fd=None, effective_ids=False, follow_symlinks=True )

Use the real uid/gid to test for access to path. Note that most operations will use the effective uid/gid, therefore this routine can be used in a suid/sgid environment to test if the invoking user has the specified access to path. mode should be F_OK to test the existence of path, or it can be the inclusive OR of one or more of R_OK, W_OK, and X_OK to test permissions. Return True if access is allowed, False if not. See the Unix man page access(2) for more information.

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"

is better written as:

try:
    fp = open("myfile")
except PermissionError:
    return "some default data"
else:
    with fp:
        return fp.read()

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.

Changed in version 3.6: Accepts a path-like object.

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.

os. chdir ( path )

Change the current working directory to path.

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.

Raises an auditing event os.chdir with argument path.

New in version 3.3: Added support for specifying path as a file descriptor on some platforms.

Changed in version 3.6: Accepts a path-like object.

os. chflags ( path, flags, *, follow_symlinks=True )

Set the flags of path to the numeric flags. flags may take a combination (bitwise OR) of the following values (as defined in the stat module):

This function can support not following symlinks.

Raises an auditing event os.chflags with arguments path, flags.

Availability: Unix.

New in version 3.3: The follow_symlinks argument.

Changed in version 3.6: Accepts a path-like object.

os. chmod ( path, mode, *, dir_fd=None, follow_symlinks=True )

Change the mode of path to the numeric mode. mode may take one of the following values (as defined in the stat module) or bitwise ORed combinations of them:

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.

Raises an auditing event os.chmod with arguments path, mode, dir_fd.

New in version 3.3: Added support for specifying path as an open file descriptor, and the dir_fd and follow_symlinks arguments.

Changed in version 3.6: Accepts a path-like object.

os. chown ( path, uid, gid, *, dir_fd=None, follow_symlinks=True )

Change the owner and group id of path to the numeric uid and gid. To leave one of the ids unchanged, set it to -1.

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.

Changed in version 3.6: Supports a path-like object.

os. chroot ( path )

Change the root directory of the current process to path.

Availability: Unix.

Changed in version 3.6: Accepts a path-like object.

os. fchdir ( fd )

Change the current working directory to the directory represented by the file descriptor fd. The descriptor must refer to