Re: os.scandir problem
Roel Schroeven <[email protected]> Tue, 31 Mar 2026 10:26:05 +0200
| Newsgroups | gmane.comp.python.general |
|---|---|
| Message-ID | <[email protected]> |
Op 31/03/2026 om 9:48 schreef Anders Munch: > Rob Cliffe via Python-list<[email protected]> wrote: > > When the RAM disc is empty, > > os.scandir("R:") > > raises a FileNotFoundError instead of (as I would have expected) returning an empty iterator. > > os.scandir is a simple wrapper around FindFirstFile/FindNextFile. If FindFirstFileW("R:", "*.*") sets the file-not-found Windows error code, then that will be turned into the exception that you see. I can confirm this. I wrote a small test directly using FindFirstFileW() (in C++, though you could do it in Python using ctypes), with the following result: (Z is an empty RAM disk, EmptyDir is a regular empty directory on my hard disk, D is an empty flash drive) Z: ERROR_FILE_NOT_FOUND Z:\ ERROR_FILE_NOT_FOUND Z:\*.* ERROR_FILE_NOT_FOUND EmptyDir OK EmptyDir\ ERROR_INVALID_PARAMETER EmptyDir\*.* OK D: ERROR_FILE_NOT_FOUND D:\ ERROR_FILE_NOT_FOUND D:\*.* OK There doesn't seem to be much consistency. As you can see, FindFirstFileW() on an empty RAM disk gives an ERROR_FILE_NOT_FOUND. Adding \ or \*.*. Doesn't help. Weirdly enough adding \*.* does help for the empty flash drive. > You might want to try os.scandir("R:\\"). Your issue could have something to do with the current directory for the R: drive not being well-defined. > I tried it; doesn't make a difference. I think your best bet is to simply handle the FileNotFoundError exception manually in your code. Perhaps best to write your own wrapper around os.scandir() that does that. -- "I've come up with a set of rules that describe our reactions to technologies: 1. Anything that is in the world when you’re born is normal and ordinary and is just a natural part of the way the world works. 2. Anything that's invented between when you’re fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. 3. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams, The Salmon of Doubt -- https://mail.python.org/mailman3//lists/python-list.python.org