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

2
  • 1
    possible duplicate of Find current directory and file's directory Commented Aug 8, 2014 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. Commented Aug 9, 2014 at 0:08

1 Answer 1

4

Use the os.getcwd() function:

>>> import os
>>> os.getcwd()
'/home/univerio'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.