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
nameattribute will be of the same type (strorbytes) 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
pathattribute will be of the same type (strorbytes) as thescandir()path argument. Usefsdecode()to decode byte filenames.
-
inode() -
Return the inode number of the entry.
The result is cached on the
DirEntryobject. Useos.stat(entry.path, follow_symlinks=False).st_inoto 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
Trueif this entry is a directory or a symbolic link pointing to a directory; returnFalseif the entry is or points to any other kind of file, or if it doesn’t exist anymore.If follow_symlinks is
False, returnTrueonly if this entry is a directory (without following symlinks); returnFalseif the entry is any other kind of file or if it doesn’t exist anymore.The result is cached on the
DirEntryobject, with a separate cache for follow_symlinksTrueandFalse. 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, butFileNotFoundErroris caught and not raised.
-
is_file(*, follow_symlinks=True) -
Return
Trueif this entry is a file or a symbolic link pointing to a file; returnFalseif 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, returnTrueonly if this entry is a file (without following symlinks); returnFalseif the entry is a directory or other non-file entry, or if it doesn’t exist anymore.The result is cached on the
DirEntryobject. Caching, system calls made, and exceptions raised are as peris_dir().
-
is_symlink() -
Return
Trueif this entry is a symbolic link (even if broken); returnFalseif the entry points to a directory or any kind of file, or if it doesn’t exist anymore.The result is cached on the
DirEntryobject. 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, butFileNotFoundErroris caught and not raised.
-
stat(*, follow_symlinks=True) -
Return a
stat_resultobject for this entry. This method follows symbolic links by default; to stat a symbolic link add thefollow_symlinks=Falseargument.On Unix, this method always requires a system call. On Windows, it only requires a system call if follow_symlinks is
Trueand the entry is a symbolic link.On Windows, the
st_ino,st_devandst_nlinkattributes of thestat_resultare always set to zero. Callos.stat()to get these attributes.The result is cached on the
DirEntryobject, with a separate cache for follow_symlinksTrueandFalse. 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.