Python Forum

Full Version: How to get calling directory?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there a way to get the calling directory in Python? I want to have the program in one spot and call it from more than one spot. The program and Python are in the path but the program works on the local directory. Help is always welcome, thanks.
You can call it by hard coding the path
You can use os.path
You can use pathlib
There are probably other ways as well
The problem is that I want the program to operate in the calling directory wherever I am.
I have a directory for each month and want it to convert data that is in the directory. Is there a way to call the program with the directory name added like
Python convert.py(this directory)
Use __file__ to get the path to the source file. For example.
from pathlib import Path


src_path = Path(__file__).parent
print(__file__, src_path)
Output:
u:\>c: C:\>python c:\users\hystadd\documents\python\sandbox\junk3.py c:\users\hystadd\documents\python\sandbox\junk3.py c:\users\hystadd\documents\python\sandbox C:\>
I will try this in the morning, thanks.