2

How do I find out the current runtime directory from within a Python script?

3 Answers 3

7

Use os.getcwd. This will tell you the current working directory. If you create a new file with

fp = open('a.txt', 'w')

then a.txt will be created in the current working directory. Use os.chdir to change the working directory, if needed.

Sign up to request clarification or add additional context in comments.

Comments

0

This is working for me.

os.path.dirname(__file__)

Comments

0

os.getcwd() will change when u use os.chdir

If you need a fixed path to your python file, use:

module_path = os.path.dirname(__file__)

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.