summaryrefslogtreecommitdiffstats
path: root/Doc/library/os.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/os.rst')
-rw-r--r--Doc/library/os.rst319
1 files changed, 258 insertions, 61 deletions
diff --git a/Doc/library/os.rst b/Doc/library/os.rst
index 7bc5989..0cc9d9c 100644
--- a/Doc/library/os.rst
+++ b/Doc/library/os.rst
@@ -78,9 +78,10 @@ uses the file system encoding to perform this conversion (see
.. versionchanged:: 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.
+ case, Python uses the :ref:`surrogateescape encoding error handler
+ <surrogateescape>`, 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
@@ -311,8 +312,6 @@ process and user.
Return the current process id.
- Availability: Unix, Windows.
-
.. function:: getppid()
@@ -549,8 +548,6 @@ process and user.
On platforms where :c:func:`strerror` returns ``NULL`` when given an unknown
error number, :exc:`ValueError` is raised.
- Availability: Unix, Windows.
-
.. data:: supports_bytes_environ
@@ -564,8 +561,6 @@ process and user.
Set the current numeric umask and return the previous umask.
- Availability: Unix, Windows.
-
.. function:: uname()
@@ -656,8 +651,6 @@ as internal buffering of data.
Close file descriptor *fd*.
- Availability: Unix, Windows.
-
.. note::
This function is intended for low-level I/O and must be applied to a file
@@ -677,8 +670,6 @@ as internal buffering of data.
except OSError:
pass
- Availability: Unix, Windows.
-
.. function:: device_encoding(fd)
@@ -695,8 +686,6 @@ as internal buffering of data.
2: stderr), the new file descriptor is :ref:`inheritable
<fd_inheritance>`.
- Availability: Unix, Windows.
-
.. versionchanged:: 3.4
The new file descriptor is now non-inheritable.
@@ -707,8 +696,6 @@ as internal buffering of data.
The file descriptor *fd2* is :ref:`inheritable <fd_inheritance>` by default,
or non-inheritable if *inheritable* is ``False``.
- Availability: Unix, Windows.
-
.. versionchanged:: 3.4
Add the optional *inheritable* parameter.
@@ -774,8 +761,6 @@ as internal buffering of data.
The :func:`.stat` function.
- Availability: Unix, Windows.
-
.. function:: fstatvfs(fd)
@@ -804,8 +789,21 @@ as internal buffering of data.
most *length* bytes in size. As of Python 3.3, this is equivalent to
``os.truncate(fd, length)``.
+ Availability: Unix, Windows.
+
+ .. versionchanged:: 3.5
+ Added support for Windows
+
+.. function:: get_blocking(fd)
+
+ Get the blocking mode of the file descriptor: ``False`` if the
+ :data:`O_NONBLOCK` flag is set, ``True`` if the flag is cleared.
+
+ See also :func:`set_blocking` and :meth:`socket.socket.setblocking`.
+
Availability: Unix.
+ .. versionadded:: 3.5
.. function:: isatty(fd)
@@ -846,8 +844,6 @@ as internal buffering of data.
current position; :const:`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.
- Availability: Unix, Windows.
-
.. data:: SEEK_SET
SEEK_CUR
@@ -856,8 +852,6 @@ as internal buffering of data.
Parameters to the :func:`lseek` function. Their values are 0, 1, and 2,
respectively.
- Availability: Unix, Windows.
-
.. versionadded:: 3.3
Some operating systems could support additional values, like
:data:`os.SEEK_HOLE` or :data:`os.SEEK_DATA`.
@@ -878,8 +872,6 @@ as internal buffering of data.
This function can support :ref:`paths relative to directory descriptors
<dir_fd>` with the *dir_fd* parameter.
- Availability: Unix, Windows.
-
.. versionchanged:: 3.4
The new file descriptor is now non-inheritable.
@@ -893,6 +885,11 @@ as internal buffering of data.
.. versionadded:: 3.3
The *dir_fd* argument.
+ .. versionchanged:: 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
+ :exc:`InterruptedError` exception (see :pep:`475` for the rationale).
+
The following constants are options for the *flags* parameter to the
:func:`~os.open` function. They can be combined using the bitwise OR operator
``|``. Some of them are not available on all platforms. For descriptions of
@@ -1060,8 +1057,6 @@ or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Window
bytes read. If the end of the file referred to by *fd* has been reached, an
empty bytes object is returned.
- Availability: Unix, Windows.
-
.. note::
This function is intended for low-level I/O and must be applied to a file
@@ -1070,6 +1065,11 @@ or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Window
:func:`popen` or :func:`fdopen`, or :data:`sys.stdin`, use its
:meth:`~file.read` or :meth:`~file.readline` methods.
+ .. versionchanged:: 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
+ :exc:`InterruptedError` exception (see :pep:`475` for the rationale).
+
.. function:: sendfile(out, in, offset, count)
sendfile(out, in, offset, count, [headers], [trailers], flags=0)
@@ -1099,9 +1099,26 @@ or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Window
Availability: Unix.
+ .. note::
+
+ For a higher-level wrapper of :func:`sendfile`, see
+ :meth:`socket.socket.sendfile`.
+
.. versionadded:: 3.3
+.. function:: set_blocking(fd, blocking)
+
+ Set the blocking mode of the specified file descriptor. Set the
+ :data:`O_NONBLOCK` flag if blocking is ``False``, clear the flag otherwise.
+
+ See also :func:`get_blocking` and :meth:`socket.socket.setblocking`.
+
+ Availability: Unix.
+
+ .. versionadded:: 3.5
+
+
.. data:: SF_NODISKIO
SF_MNOWAIT
SF_SYNC
@@ -1158,8 +1175,6 @@ or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Window
Write the bytestring in *str* to file descriptor *fd*. Return the number of
bytes actually written.
- Availability: Unix, Windows.
-
.. note::
This function is intended for low-level I/O and must be applied to a file
@@ -1168,6 +1183,11 @@ or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Window
:func:`fdopen`, or :data:`sys.stdout` or :data:`sys.stderr`, use its
:meth:`~file.write` method.
+ .. versionchanged:: 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
+ :exc:`InterruptedError` exception (see :pep:`475` for the rationale).
+
.. function:: writev(fd, buffers)
@@ -1330,8 +1350,6 @@ features:
or not it is available using :data:`os.supports_effective_ids`. If it is
unavailable, using it will raise a :exc:`NotImplementedError`.
- Availability: Unix, Windows.
-
.. note::
Using :func:`access` to check if a user is authorized to e.g. open a file
@@ -1384,8 +1402,6 @@ features:
This function can support :ref:`specifying a file descriptor <path_fd>`. The
descriptor must refer to an opened directory, not an open file.
- Availability: Unix, Windows.
-
.. versionadded:: 3.3
Added support for specifying *path* as a file descriptor
on some platforms.
@@ -1447,8 +1463,6 @@ features:
:ref:`paths relative to directory descriptors <dir_fd>` and :ref:`not
following symlinks <follow_symlinks>`.
- Availability: Unix, Windows.
-
.. note::
Although Windows supports :func:`chmod`, you can only set the file's
@@ -1499,15 +1513,11 @@ features:
Return a string representing the current working directory.
- Availability: Unix, Windows.
-
.. function:: getcwdb()
Return a bytestring representing the current working directory.
- Availability: Unix, Windows.
-
.. function:: lchflags(path, flags)
@@ -1570,7 +1580,11 @@ features:
.. note::
To encode ``str`` filenames to ``bytes``, use :func:`~os.fsencode`.
- Availability: Unix, Windows.
+ .. seealso::
+
+ The :func:`scandir` function returns directory entries along with
+ file attribute information, giving better performance for many
+ common use cases.
.. versionchanged:: 3.2
The *path* parameter became optional.
@@ -1619,8 +1633,6 @@ features:
It is also possible to create temporary directories; see the
:mod:`tempfile` module's :func:`tempfile.mkdtemp` function.
- Availability: Unix, Windows.
-
.. versionadded:: 3.3
The *dir_fd* argument.
@@ -1779,8 +1791,6 @@ features:
This function is identical to :func:`unlink`.
- Availability: Unix, Windows.
-
.. versionadded:: 3.3
The *dir_fd* argument.
@@ -1814,8 +1824,6 @@ features:
If you want cross-platform overwriting of the destination, use :func:`replace`.
- Availability: Unix, Windows.
-
.. versionadded:: 3.3
The *src_dir_fd* and *dst_dir_fd* arguments.
@@ -1844,8 +1852,6 @@ features:
This function can support specifying *src_dir_fd* and/or *dst_dir_fd* to
supply :ref:`paths relative to directory descriptors <dir_fd>`.
- Availability: Unix, Windows.
-
.. versionadded:: 3.3
@@ -1858,12 +1864,180 @@ features:
This function can support :ref:`paths relative to directory descriptors
<dir_fd>`.
- Availability: Unix, Windows.
-
.. versionadded:: 3.3
The *dir_fd* parameter.
+.. function:: scandir(path='.')
+
+ Return an iterator of :class:`DirEntry` objects corresponding to the entries
+ in the directory given by *path*. The entries are yielded in arbitrary
+ order, and the special entries ``'.'`` and ``'..'`` are not included.
+
+ Using :func:`scandir` instead of :func:`listdir` can significantly
+ increase the performance of code that also needs file type or file
+ attribute information, because :class:`DirEntry` objects expose this
+ information if the operating system provides it when scanning a directory.
+ All :class:`DirEntry` methods may perform a system call, but
+ :func:`~DirEntry.is_dir` and :func:`~DirEntry.is_file` usually only
+ require a system call for symbolic links; :func:`DirEntry.stat`
+ always requires a system call on Unix but only requires one for
+ symbolic links on Windows.
+
+ On Unix, *path* can be of type :class:`str` or :class:`bytes` (use
+ :func:`~os.fsencode` and :func:`~os.fsdecode` to encode and decode
+ :class:`bytes` paths). On Windows, *path* must be of type :class:`str`.
+ On both sytems, the type of the :attr:`~DirEntry.name` and
+ :attr:`~DirEntry.path` attributes of each :class:`DirEntry` will be of
+ the same type as *path*.
+
+ The following example shows a simple use of :func:`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::
+
+ for entry in os.scandir(path):
+ if not entry.name.startswith('.') and entry.is_file():
+ print(entry.name)
+
+ .. note::
+
+ On Unix-based systems, :func:`scandir` uses the system's
+ `opendir() <http://pubs.opengroup.org/onlinepubs/009695399/functions/opendir.html>`_
+ and
+ `readdir() <http://pubs.opengroup.org/onlinepubs/009695399/functions/readdir_r.html>`_
+ functions. On Windows, it uses the Win32
+ `FindFirstFileW <http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx>`_
+ and
+ `FindNextFileW <http://msdn.microsoft.com/en-us/library/windows/desktop/aa364428(v=vs.85).aspx>`_
+ functions.
+
+ .. versionadded:: 3.5
+
+
+.. class:: DirEntry
+
+ Object yielded by :func:`scandir` to expose the file path and other file
+ attributes of a directory entry.
+
+ :func:`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 ``DirEntry`` object will cache the result.
+
+ ``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 :func:`scandir`, call ``os.stat(entry.path)`` to fetch
+ up-to-date information.
+
+ Because the ``DirEntry`` methods can make operating system calls, they may
+ also raise :exc:`OSError`. If you need very fine-grained
+ control over errors, you can catch :exc:`OSError` when calling one of the
+ ``DirEntry`` methods and handle as appropriate.
+
+ Attributes and methods on a ``DirEntry`` instance are as follows:
+
+ .. attribute:: name
+
+ The entry's base filename, relative to the :func:`scandir` *path*
+ argument.
+
+ The :attr:`name` attribute will be of the same type (``str`` or
+ ``bytes``) as the :func:`scandir` *path* argument. Use
+ :func:`~os.fsdecode` to decode byte filenames.
+
+ .. attribute:: path
+
+ The entry's full path name: equivalent to ``os.path.join(scandir_path,
+ entry.name)`` where *scandir_path* is the :func:`scandir` *path*
+ argument. The path is only absolute if the :func:`scandir` *path*
+ argument was absolute.
+
+ The :attr:`path` attribute will be of the same type (``str`` or
+ ``bytes``) as the :func:`scandir` *path* argument. Use
+ :func:`~os.fsdecode` to decode byte filenames.
+
+ .. method:: inode()
+
+ Return the inode number of the entry.
+
+ The result is cached on the ``DirEntry`` object, use ``os.stat(entry.path,
+ follow_symlinks=False).st_ino`` to fetch up-to-date information.
+
+ On Unix, no system call is required.
+
+ .. method:: is_dir(\*, follow_symlinks=True)
+
+ If *follow_symlinks* is ``True`` (the default), return ``True`` if the
+ entry is a directory or a symbolic link pointing to a directory;
+ return ``False`` if it is or points to any other kind of file, or if it
+ doesn't exist anymore.
+
+ If *follow_symlinks* is ``False``, return ``True`` only if this entry
+ is a directory; return ``False`` if it is any other kind of file
+ or if it doesn't exist anymore.
+
+ The result is cached on the ``DirEntry`` object. Call :func:`os.stat`
+ along with :func:`stat.S_ISDIR` to fetch up-to-date information.
+
+ This method can raise :exc:`OSError`, such as :exc:`PermissionError`,
+ but :exc:`FileNotFoundError` is caught and not raised.
+
+ In most cases, no system call is required.
+
+ .. method:: is_file(\*, follow_symlinks=True)
+
+ If *follow_symlinks* is ``True`` (the default), return ``True`` if the
+ entry is a file or a symbolic link pointing to a file; return ``False``
+ if it is or points to a directory or other non-file entry, or if it
+ doesn't exist anymore.
+
+ If *follow_symlinks* is ``False``, return ``True`` only if this entry
+ is a file; return ``False`` if it is a directory or other non-file entry,
+ or if it doesn't exist anymore.
+
+ The result is cached on the ``DirEntry`` object. Call :func:`os.stat`
+ along with :func:`stat.S_ISREG` to fetch up-to-date information.
+
+ This method can raise :exc:`OSError`, such as :exc:`PermissionError`,
+ but :exc:`FileNotFoundError` is caught and not raised.
+
+ In most cases, no system call is required.
+
+ .. method:: is_symlink()
+
+ Return ``True`` if this entry is a symbolic link (even if broken);
+ return ``False`` if it points to a directory or any kind of file,
+ or if it doesn't exist anymore.
+
+ The result is cached on the ``DirEntry`` object. Call
+ :func:`os.path.islink` to fetch up-to-date information.
+
+ The method can raise :exc:`OSError`, such as :exc:`PermissionError`,
+ but :exc:`FileNotFoundError` is caught and not raised.
+
+ In most cases, no system call is required.
+
+ .. method:: stat(\*, follow_symlinks=True)
+
+ Return a :class:`stat_result` object for this entry. This method
+ follows symbolic links by default; to stat a symbolic link add the
+ ``follow_symlinks=False`` argument.
+
+ On Unix, this method always requires a system call. On Windows,
+ ``DirEntry.stat()`` requires a system call only if the
+ entry is a symbolic link, and ``DirEntry.stat(follow_symlinks=False)``
+ never requires a system call.
+
+ On Windows, the ``st_ino``, ``st_dev`` and ``st_nlink`` attributes of the
+ :class:`stat_result` are always set to zero. Call :func:`os.stat` to
+ get these attributes.
+
+ The result is cached on the ``DirEntry`` object. Call :func:`os.stat`
+ to fetch up-to-date information.
+
+ .. versionadded:: 3.5
+
+
.. function:: stat(path, \*, dir_fd=None, follow_symlinks=True)
Get the status of a file or a file descriptor. Perform the equivalent of a
@@ -1890,8 +2064,6 @@ features:
>>> statinfo.st_size
264
- Availability: Unix, Windows.
-
.. seealso::
:func:`fstat` and :func:`lstat` functions.
@@ -2039,6 +2211,15 @@ features:
File type.
+ On Windows systems, the following attribute is also available:
+
+ .. attribute:: st_file_attributes
+
+ Windows file attributes: ``dwFileAttributes`` member of the
+ ``BY_HANDLE_FILE_INFORMATION`` structure returned by
+ :c:func:`GetFileInformationByHandle`. See the ``FILE_ATTRIBUTE_*``
+ constants in the :mod:`stat` module.
+
The standard module :mod:`stat` defines functions and constants that are
useful for extracting information from a :c:type:`stat` structure. (On
Windows, some items are filled with dummy values.)
@@ -2056,6 +2237,9 @@ features:
Added the :attr:`st_atime_ns`, :attr:`st_mtime_ns`, and
:attr:`st_ctime_ns` members.
+ .. versionadded:: 3.5
+ Added the :attr:`st_file_attributes` member on Windows.
+
.. function:: stat_float_times([newvalue])
@@ -2259,10 +2443,12 @@ features:
This function can support :ref:`specifying a file descriptor <path_fd>`.
- Availability: Unix.
+ Availability: Unix, Windows.
.. versionadded:: 3.3
+ .. versionchanged:: 3.5
+ Added support for Windows
.. function:: unlink(path, *, dir_fd=None)
@@ -2271,8 +2457,6 @@ features:
name. Please see the documentation for :func:`remove` for
further information.
- Availability: Unix, Windows.
-
.. versionadded:: 3.3
The *dir_fd* parameter.
@@ -2309,8 +2493,6 @@ features:
:ref:`paths relative to directory descriptors <dir_fd>` and :ref:`not
following symlinks <follow_symlinks>`.
- Availability: Unix, Windows.
-
.. versionadded:: 3.3
Added support for specifying an open file descriptor for *path*,
and the *dir_fd*, *follow_symlinks*, and *ns* parameters.
@@ -2401,6 +2583,10 @@ features:
for name in dirs:
os.rmdir(os.path.join(root, name))
+ .. versionchanged:: 3.5
+ This function now calls :func:`os.scandir` instead of :func:`os.listdir`,
+ making it faster by reducing the number of calls to :func:`os.stat`.
+
.. function:: fwalk(top='.', topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None)
@@ -2557,8 +2743,6 @@ to be ignored.
Python signal handler registered for :const:`SIGABRT` with
:func:`signal.signal`.
- Availability: Unix, Windows.
-
.. function:: execl(path, arg0, arg1, ...)
execle(path, arg0, arg1, ..., env)
@@ -2622,8 +2806,6 @@ to be ignored.
Exit the process with status *n*, without calling cleanup handlers, flushing
stdio buffers, etc.
- Availability: Unix, Windows.
-
.. note::
The standard way to exit is ``sys.exit(n)``. :func:`_exit` should
@@ -2986,6 +3168,10 @@ written in Python, such as a mail server's external command delivery program.
doesn't work if it is. Use the :func:`os.path.normpath` function to ensure that
the path is properly encoded for Win32.
+ To reduce interpreter startup overhead, the Win32 :c:func:`ShellExecute`
+ function is not resolved until this function is first called. If the function
+ cannot be resolved, :exc:`NotImplementedError` will be raised.
+
Availability: Windows.
@@ -3133,6 +3319,11 @@ written in Python, such as a mail server's external command delivery program.
id is known, not necessarily a child process. The :func:`spawn\* <spawnl>`
functions called with :const:`P_NOWAIT` return suitable process handles.
+ .. versionchanged:: 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
+ :exc:`InterruptedError` exception (see :pep:`475` for the rationale).
+
.. function:: wait3(options)
@@ -3540,3 +3731,9 @@ Miscellaneous Functions
For an easy-to-use interface to the random number generator
provided by your platform, please see :class:`random.SystemRandom`.
+
+ .. versionchanged:: 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.