tags:

views:

35

answers:

3

I have this program that at one point accesses os.getcwd(), but some times, depending on where the program has gone before getting to that line, it crashes with the message "OSError: [Errno 2] No such file or directory". I cannot figure out what i can do wrong then calling os.getcwd(). There's no arguments, and there should always be a current working directory, right?

+1  A: 

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.

David Zaslavsky
+2  A: 

The current directory may have been deleted by another process.

Marco Mariani
Thanks, that was it. Somewhere i did some unnecessary changing of workdir, deleted it later somewhere, and then forgot to change back.
Eskil
A: 

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

gauteh