On this page
os.DirEntry
Prev: os.cpu_count | Up: os | Next: os.dup
Rel: os.abort, os.access, os.chdir, os.chmod
Ref: docs.python.org — os.DirEntry
Tags: #public #python3 #stdlib #os
Import
import os
from os import DirEntry
Signature & Overview
os.DirEntry()
os.DirEntry class.
Methods & Attributes
DirEntry.inode(self, /)— Return inode of the entry; cached per entry.DirEntry.is_dir(self, /, *, follow_symlinks=True)— Return True if the entry is a directory; cached per entry.DirEntry.is_file(self, /, *, follow_symlinks=True)— Return True if the entry is a file; cached per entry.DirEntry.is_junction(self, /)— Return True if the entry is a junction; cached per entry.DirEntry.is_symlink(self, /)— Return True if the entry is a symbolic link; cached per entry.DirEntry.stat(self, /, *, follow_symlinks=True)— Return stat_result object for the entry; cached per entry.
Usage
import os
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
(Path(tmp) / "a.txt").write_text("hi", encoding="utf-8")
with os.scandir(tmp) as it:
entries = list(it)
assert len(entries) == 1 and isinstance(entries[0], os.DirEntry)
assert entries[0].name == "a.txt" and entries[0].is_file()
Gotchas
- Yielded by
os.scandir(): Caches file type and stat metadata from the directory listing syscall, makingentry.is_file()/entry.stat()much faster thanos.listdir()+os.stat().
REPL Inspect
from os import DirEntry
help(DirEntry)
Linked from
Related notes
- tuts/python3/stdlib_module/os/cpu_count Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/dup Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/os/abort Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/access Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chdir Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chmod Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chown shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/close shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/dup2 shared tags: #os, #stdlib