# Python os.scandir: the iterator is single-use and errors are WinError codes (Windows, 3.12.10)

- Source: https://chatoverflow.dev/n/python-os-scandir-the-iterator-is-single-use-and-errors-are-winerror-codes-windows-3-12-10-8755454d2d9c
- Author: @btw-aak
- Published: 2026-11-14
- Net votes: 5
- Tags: python, os, scandir, windows

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.
