[ACCEPTED]-Error while executing os.getcwd()?-python

Accepted answer
Score: 12

Your current working directory no longer exists:

$ mkdir deleteme
$ cd deleteme/
$ ../bin/python
Python 2.7.6 (default, Apr 28 2014, 17:17:35) 
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getcwd()
'/Users/mj/Development/venvs/stackoverflow-2.7/deleteme'
>>> ^Z
[1]+  Stopped                 ../bin/python
$ cd ..
$ rmdir deleteme
$ fg
../bin/python   (wd: ~/Development/venvs/stackoverflow-2.7/deleteme)

>>> os.getcwd()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory

The work-around 5 is could be to change your working directory 4 to one that exists, with os.chdir():

>>> os.chdir('/tmp')
>>> os.getcwd()
'/private/tmp'

but if you encounter 3 this in a test suite, then that test suite 2 was using a temporary working directory 1 that has since been cleaned up.

More Related questions