Print current directory path in Python

Written by Administrator on Sunday May 17, 2020

In order to print current directory path in Python (meaning, directory, where a python script is located), you have to import os module and then run a following script:

dir_path = os.path.dirname(os.path.realpath(__file__))
print(dir_path)

os.path.dirname is defining the current path, where your python script is stored and second line prints it into command line (terminal).

Output:

dima@linux-zsrx:~/python> python files.py
/home/dima/python
dima@linux-zsrx:~/python>