summaryrefslogtreecommitdiff
path: root/Doc/library/os.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/os.rst')
-rw-r--r--Doc/library/os.rst254
1 files changed, 250 insertions, 4 deletions
diff --git a/Doc/library/os.rst b/Doc/library/os.rst
index 41efccd980..bd09937aeb 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
@@ -804,8 +805,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)
@@ -893,6 +907,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
@@ -1070,6 +1089,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, nbytes)
sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)
@@ -1107,6 +1131,18 @@ or `the MSDN <http://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx>`_ on Window
.. 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
@@ -1173,6 +1209,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)
@@ -1577,6 +1618,11 @@ features:
Availability: Unix, Windows.
+ .. seealso::
+
+ The :func:`scandir` function returns the directory entries with more
+ information than just the name.
+
.. versionchanged:: 3.2
The *path* parameter became optional.
@@ -1869,6 +1915,178 @@ features:
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.
+
+ On Windows, *path* must of type :class:`str`. On POSIX, *path* can be of
+ type :class:`str` or :class:`bytes`. If *path* is of type :class:`bytes`,
+ the :attr:`~DirEntry.name` and :attr:`~DirEntry.path` attributes of
+ :class:`DirEntry` are also of type ``bytes``. Use :func:`~os.fsencode` and
+ :func:`~os.fsdecode` to encode and decode paths.
+
+ The :func:`scandir` function is recommended, instead of :func:`listdir`,
+ when the file type of entries is used. In most cases, the file type of a
+ :class:`DirEntry` is retrieved directly by :func:`scandir`, no system call
+ is required. If only the name of entries is used, :func:`listdir` can
+ be more efficient than :func:`scandir`.
+
+ 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
+ ``'.'``::
+
+ 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.
+
+ .. seealso::
+
+ The :func:`listdir` function returns the names of the directory entries.
+
+ .. 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 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`. For example, if a file is deleted between calling
+ :func:`scandir` and calling :func:`DirEntry.stat`, a
+ :exc:`FileNotFoundError` exception can be raised. Unfortunately, the
+ behaviour on errors depends on the platform. 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` type is :class:`str`. On POSIX, it can be of type
+ :class:`bytes` if the type of the :func:`scandir` *path* argument is also
+ :class:`bytes`. Use :func:`~os.fsdecode` to decode the name.
+
+ .. 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 is absolute.
+
+ The :attr:`name` type is :class:`str`. On POSIX, it can be of type
+ :class:`bytes` if the type of the :func:`scandir` *path* argument is also
+ :class:`bytes`. Use :func:`~os.fsdecode` to decode the path.
+
+ .. method:: inode()
+
+ Return the inode number of the entry.
+
+ The result is cached in the object, use ``os.stat(entry.path,
+ follow_symlinks=False).st_ino`` to fetch up-to-date information.
+
+ On POSIX, 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 points to another kind of file, if it doesn't
+ exist anymore or if it is a broken symbolic link.
+
+ If *follow_symlinks* is ``False``, return ``True`` only if this entry
+ is a directory, return ``False`` if it points to a symbolic link or
+ another kind of file, if the entry doesn't exist anymore or if it is a
+ broken symbolic link
+
+ The result is cached in the object. Call :func:`stat.S_ISDIR` with
+ :func:`os.stat` to fetch up-to-date information.
+
+ The method can raise :exc:`OSError`, such as :exc:`PermissionError`,
+ but :exc:`FileNotFoundError` is catched.
+
+ 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 regular file or a symbolic link pointing to a regular file,
+ return ``False`` if it points to another kind of file, if it doesn't
+ exist anymore or if it is a broken symbolic link.
+
+ If *follow_symlinks* is ``False``, return ``True`` only if this entry
+ is a regular file, return ``False`` if it points to a symbolic link or
+ another kind of file, if it doesn't exist anymore or if it is a broken
+ symbolic link.
+
+ The result is cached in the object. Call :func:`stat.S_ISREG` with
+ :func:`os.stat` to fetch up-to-date information.
+
+ The method can raise :exc:`OSError`, such as :exc:`PermissionError`,
+ but :exc:`FileNotFoundError` is catched.
+
+ In most cases, no system call is required.
+
+ .. method:: is_symlink()
+
+ Return ``True`` if this entry is a symbolic link or a broken symbolic
+ link, return ``False`` if it points to a another kind of file or if the
+ entry doesn't exist anymore.
+
+ The result is cached in the 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 catched.
+
+ In most cases, no system call is required.
+
+ .. method:: stat(\*, follow_symlinks=True)
+
+ Return a :class:`stat_result` object for this entry. This function
+ normally follows symbolic links; to stat a symbolic link add the
+ argument ``follow_symlinks=False``.
+
+ 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 in the object. Call :func:`os.stat` to fetch
+ up-to-date information.
+
+ On Windows, ``DirEntry.stat(follow_symlinks=False)`` doesn't require a
+ system call. ``DirEntry.stat()`` requires a system call if the entry is a
+ symbolic link.
+
+ .. 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
@@ -2044,6 +2262,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.)
@@ -2061,6 +2288,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])
@@ -2264,10 +2494,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)
@@ -2406,6 +2638,11 @@ features:
for name in dirs:
os.rmdir(os.path.join(root, name))
+ .. versionchanged:: 3.5
+ The function now calls :func:`os.scandir` instead of :func:`os.listdir`.
+ The usage of :func:`os.scandir` reduces the number of calls to
+ :func:`os.stat`.
+
.. function:: fwalk(top='.', topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None)
@@ -2990,6 +3227,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.
@@ -3137,6 +3378,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)