2

os.path.dirname(os.path.realpath(__file__)) will find your current working directory, but if I'm running the python shell, __file__ is undefined, so that method throws an exception.

How can you find your current working directory while inside a Python shell?

Running: Windows 7, Python 3.3, VirtualEnv

AllTradesJack
  • 2,762
  • 5
  • 25
  • 36
  • 1
    possible duplicate of [Find current directory and file's directory](http://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory) – TheSoundDefense Aug 08 '14 at 23:58
  • That won't find your current working directory in a script either. It'll find the directory the script is in. So, for example, if I'm in `C:\Users\me`, and I run `C:\Python33\python D:\MyStuff\MyScript.py`, the current working directory is `C:\Users\me`, but you're going to get `D:\MyStuff`. – abarnert Aug 09 '14 at 00:08

1 Answers1

4

Use the os.getcwd() function:

>>> import os
>>> os.getcwd()
'/home/univerio'
univerio
  • 19,548
  • 3
  • 66
  • 68