Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while executing os.getcwd()?

Tags:

python

I encountered a rather weird problem to me. I stepped into some tests to debug what's going wrong, and when I try to find the current working directory, I get the following error:

ipdb> os.getcwd()
*** OSError: [Errno 2] No such file or directory

What's the problem and how can I view the current working directory?

like image 558
linkyndy Avatar asked May 27 '14 12:05

linkyndy


1 Answers

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 is could be to change your working directory to one that exists, with os.chdir():

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

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

like image 179
Martijn Pieters Avatar answered Oct 18 '22 22:10

Martijn Pieters