Python: Why does os.getcwd() sometimes crash with OSError?

You would get that error if your current directory no longer exists (it is deleted).


The current directory may have been deleted by another process.


You might get that error if the current working directory has been deleted. Programs that are working in a particular directory don't automatically notice if the directory gets deleted; as far as the program is concerned, the CWD is just a string, at least until you do something like os.getcwd() that actually accesses that path on the filesystem. So it's possible to have a current directory that doesn't exist.

Without knowing more about your program and its execution environment, I couldn't tell you if that is what's actually happening, though.

Tags:

Python