Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f0c5d9

Browse files
committed
Enhance os.scandir() doc
Issue #26248, patch written by Ben Hoyt: 1) Clarify that the return values of is_dir()/is_file()/etc are cached separately for follow_symlinks True and False. 2) Be more specific about when the functions require a system call, and how it relates to caching and follow_symlinks. 3) DRY up common stuff between is_dir and is_file by saying "Caching, system calls made, and exceptions raised are as per is_dir" in is_file. 4) Tweak to the first paragraph of docs for is_dir/is_file to simplify: assume the follow_symlinks=True default, then note the follow_symlinks=False non-default case after.
1 parent fe5f614 commit 5f0c5d9

1 file changed

Lines changed: 38 additions & 35 deletions

File tree

Doc/library/os.rst

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,80 +1960,83 @@ features:
19601960

19611961
Return the inode number of the entry.
19621962

1963-
The result is cached on the ``DirEntry`` object, use ``os.stat(entry.path,
1963+
The result is cached on the ``DirEntry`` object. Use ``os.stat(entry.path,
19641964
follow_symlinks=False).st_ino`` to fetch up-to-date information.
19651965

1966-
On Unix, no system call is required.
1966+
On the first, uncached call, a system call is required on Windows but
1967+
not on Unix.
19671968

19681969
.. method:: is_dir(\*, follow_symlinks=True)
19691970

1970-
If *follow_symlinks* is ``True`` (the default), return ``True`` if the
1971-
entry is a directory or a symbolic link pointing to a directory;
1972-
return ``False`` if it is or points to any other kind of file, or if it
1973-
doesn't exist anymore.
1971+
Return ``True`` if this entry is a directory or a symbolic link pointing
1972+
to a directory; return ``False`` if the entry is or points to any other
1973+
kind of file, or if it doesn't exist anymore.
19741974

19751975
If *follow_symlinks* is ``False``, return ``True`` only if this entry
1976-
is a directory; return ``False`` if it is any other kind of file
1977-
or if it doesn't exist anymore.
1976+
is a directory (without following symlinks); return ``False`` if the
1977+
entry is any other kind of file or if it doesn't exist anymore.
1978+
1979+
The result is cached on the ``DirEntry`` object, with a separate cache
1980+
for *follow_symlinks* ``True`` and ``False``. Call :func:`os.stat` along
1981+
with :func:`stat.S_ISDIR` to fetch up-to-date information.
19781982

1979-
The result is cached on the ``DirEntry`` object. Call :func:`os.stat`
1980-
along with :func:`stat.S_ISDIR` to fetch up-to-date information.
1983+
On the first, uncached call, no system call is required in most cases.
1984+
Specifically, for non-symlinks, neither Windows or Unix require a system
1985+
call, except on certain Unix file systems, such as network file systems,
1986+
that return ``dirent.d_type == DT_UNKNOWN``. If the entry is a symlink,
1987+
a system call will be required to follow the symlink unless
1988+
*follow_symlinks* is ``False``.
19811989

19821990
This method can raise :exc:`OSError`, such as :exc:`PermissionError`,
19831991
but :exc:`FileNotFoundError` is caught and not raised.
19841992

1985-
In most cases, no system call is required.
1986-
19871993
.. method:: is_file(\*, follow_symlinks=True)
19881994

1989-
If *follow_symlinks* is ``True`` (the default), return ``True`` if the
1990-
entry is a file or a symbolic link pointing to a file; return ``False``
1991-
if it is or points to a directory or other non-file entry, or if it
1992-
doesn't exist anymore.
1995+
Return ``True`` if this entry is a file or a symbolic link pointing to a
1996+
file; return ``False`` if the entry is or points to a directory or other
1997+
non-file entry, or if it doesn't exist anymore.
19931998

19941999
If *follow_symlinks* is ``False``, return ``True`` only if this entry
1995-
is a file; return ``False`` if it is a directory or other non-file entry,
1996-
or if it doesn't exist anymore.
2000+
is a file (without following symlinks); return ``False`` if the entry is
2001+
a directory or other non-file entry, or if it doesn't exist anymore.
19972002

1998-
The result is cached on the ``DirEntry`` object. Call :func:`os.stat`
1999-
along with :func:`stat.S_ISREG` to fetch up-to-date information.
2000-
2001-
This method can raise :exc:`OSError`, such as :exc:`PermissionError`,
2002-
but :exc:`FileNotFoundError` is caught and not raised.
2003-
2004-
In most cases, no system call is required.
2003+
The result is cached on the ``DirEntry`` object. Caching, system calls
2004+
made, and exceptions raised are as per :func:`~DirEntry.is_dir`.
20052005

20062006
.. method:: is_symlink()
20072007

20082008
Return ``True`` if this entry is a symbolic link (even if broken);
2009-
return ``False`` if it points to a directory or any kind of file,
2009+
return ``False`` if the entry points to a directory or any kind of file,
20102010
or if it doesn't exist anymore.
20112011

20122012
The result is cached on the ``DirEntry`` object. Call
20132013
:func:`os.path.islink` to fetch up-to-date information.
20142014

2015-
The method can raise :exc:`OSError`, such as :exc:`PermissionError`,
2016-
but :exc:`FileNotFoundError` is caught and not raised.
2015+
On the first, uncached call, no system call is required in most cases.
2016+
Specifically, neither Windows or Unix require a system call, except on
2017+
certain Unix file systems, such as network file systems, that return
2018+
``dirent.d_type == DT_UNKNOWN``.
20172019

2018-
In most cases, no system call is required.
2020+
This method can raise :exc:`OSError`, such as :exc:`PermissionError`,
2021+
but :exc:`FileNotFoundError` is caught and not raised.
20192022

20202023
.. method:: stat(\*, follow_symlinks=True)
20212024

20222025
Return a :class:`stat_result` object for this entry. This method
20232026
follows symbolic links by default; to stat a symbolic link add the
20242027
``follow_symlinks=False`` argument.
20252028

2026-
On Unix, this method always requires a system call. On Windows,
2027-
``DirEntry.stat()`` requires a system call only if the
2028-
entry is a symbolic link, and ``DirEntry.stat(follow_symlinks=False)``
2029-
never requires a system call.
2029+
On Unix, this method always requires a system call. On Windows, it
2030+
only requires a system call if *follow_symlinks* is ``True`` and the
2031+
entry is a symbolic link.
20302032

20312033
On Windows, the ``st_ino``, ``st_dev`` and ``st_nlink`` attributes of the
20322034
:class:`stat_result` are always set to zero. Call :func:`os.stat` to
20332035
get these attributes.
20342036

2035-
The result is cached on the ``DirEntry`` object. Call :func:`os.stat`
2036-
to fetch up-to-date information.
2037+
The result is cached on the ``DirEntry`` object, with a separate cache
2038+
for *follow_symlinks* ``True`` and ``False``. Call :func:`os.stat` to
2039+
fetch up-to-date information.
20372040

20382041
Note that there is a nice correspondence between several attributes
20392042
and methods of ``DirEntry`` and of :class:`pathlib.Path`. In

0 commit comments

Comments
 (0)