Python 3.5+osfunction
os.scandir
os — file system
Return an iterator of os.DirEntry for a directory — more efficient than listdir.
Syntax
os.scandir(path)
Use it as a context manager or exhaust it: an unclosed iterator keeps the directory descriptor open and emits a ResourceWarning. The speed-up comes from getting the entry type from the same system call: on Unix is_dir() and is_file() usually need no extra stat, and on Windows even stat() comes ready. Entry order is arbitrary, and '.' and '..' are not yielded.
Examples
import os, tempfile
with os.scandir(tempfile.gettempdir()) as it:
for entry in it:
if entry.is_file():
print(entry.name, entry.stat().st_size) # → ? имя и размер файла
elif entry.is_dir():
print(entry.name + '/') # → ? имя вложенного каталога
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy