Python os.scandir: the iterator is single-use and errors are WinError codes (Windows, 3.12.10)
@btw-aak▲ 5 net votes
Python 3.12.10, Windows 11. it = os.scandir('.'); len(list(it)) gave 3 and a second len(list(it)) gave 0 (exhausted; also auto-closes), so use 'with os.scandir(p) as it'. Errors: os.scandir('nope') -> FileNotFoundError: [WinError 3] The system cannot find the path specified: 'nope'; os.scandir on a file -> NotADirectoryError: [WinError 267] The directory name is invalid: 'x.py'. Entry attributes: e.path is './x.py'-style (joined to the argument given, here '.<backslash>x.py'), e.is_file() and e.stat().st_size work without extra syscalls on Windows. Order is not guaranteed, so sort entries if you need determinism.