class os.DirEntry
Object yielded by scandir()
to expose the file path and other file attributes of a directory entry.
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 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 OSError
. If you need very fine-grained control over errors, you can catch OSError
when calling one of the DirEntry
methods and handle as appropriate.
Attributes and methods on a DirEntry
instance are as follows:
-
name
-
The entry’s base filename, relative to the
scandir()
path argument.The
name
attribute will be of the same type (str
orbytes
) as thescandir()
path argument. Usefsdecode()
to decode byte filenames.
-
path
-
The entry’s full path name: equivalent to
os.path.join(scandir_path, entry.name)
where scandir_path is thescandir()
path argument. The path is only absolute if thescandir()
path argument was absolute.The
path
attribute will be of the same type (str
orbytes
) as thescandir()
path argument. Usefsdecode()
to decode byte filenames.
-
inode()
-
Return the inode number of the entry.
The result is cached on the
DirEntry
object. Useos.stat(entry.path, follow_symlinks=False).st_ino
to fetch up-to-date information.On the first, uncached call, a system call is required on Windows but not on Unix.
-
is_dir(*, follow_symlinks=True)
-
Return
True
if this entry is a directory or a symbolic link pointing to a directory; returnFalse
if the entry is or points to any other kind of file, or if it doesn’t exist anymore.If follow_symlinks is
False
, returnTrue
only if this entry is a directory (without following symlinks); returnFalse
if the entry is any other kind of file or if it doesn’t exist anymore.The result is cached on the
DirEntry
object, with a separate cache for follow_symlinksTrue
andFalse
. Callos.stat()
along withstat.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 isFalse
.This method can raise
OSError
, such asPermissionError
, butFileNotFoundError
is caught and not raised.
-
is_file(*, follow_symlinks=True)
-
Return
True
if this entry is a file or a symbolic link pointing to a file; returnFalse
if the entry is or points to a directory or other non-file entry, or if it doesn’t exist anymore.If follow_symlinks is
False
, returnTrue
only if this entry is a file (without following symlinks); returnFalse
if the entry is a directory or other non-file entry, or if it doesn’t exist anymore.The result is cached on the
DirEntry
object. Caching, system calls made, and exceptions raised are as peris_dir()
.
-
is_symlink()
-
Return
True
if this entry is a symbolic link (even if broken); returnFalse
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
DirEntry
object. Callos.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 asPermissionError
, butFileNotFoundError
is caught and not raised.
-
stat(*, follow_symlinks=True)
-
Return a
stat_result
object for this entry. This method follows symbolic links by default; to stat a symbolic link add thefollow_symlinks=False
argument.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 symbolic link.On Windows, the
st_ino
,st_dev
andst_nlink
attributes of thestat_result
are always set to zero. Callos.stat()
to get these attributes.The result is cached on the
DirEntry
object, with a separate cache for follow_symlinksTrue
andFalse
. Callos.stat()
to fetch up-to-date information.
Note that there is a nice correspondence between several attributes and methods of 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.
New in version 3.5.
Please login to continue.