How do I get the path of the Python script I am running in? [duplicate] How do I get the path of the Python script I am running in? [duplicate] python python

How do I get the path of the Python script I am running in? [duplicate]


Use this to get the path of the current file. It will resolve any symlinks in the path.

import osfile_path = os.path.realpath(__file__)

This works fine on my mac. It won't work from the Python interpreter (you need to be executing a Python file).


import osprint os.path.abspath(__file__)


7.2 of Dive Into Python: Finding the Path.

import sys, osprint('sys.argv[0] =', sys.argv[0])             pathname = os.path.dirname(sys.argv[0])        print('path =', pathname)print('full path =', os.path.abspath(pathname)) 


matomo