Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
How do I find out the current runtime directory from within a Python script?
Use os.getcwd. This will tell you the current working directory. If you create a new file with
os.getcwd
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.
a.txt
os.chdir
Add a comment
This is working for me.
os.path.dirname(__file__)
os.getcwd() will change when u use os.chdir
os.getcwd()
If you need a fixed path to your python file, use:
module_path = os.path.dirname(__file__)
Required, but never shown
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.
Explore related questions
See similar questions with these tags.