How do I get the path and name of the file that is currently executing? How do I get the path and name of the file that is currently executing? python python

How do I get the path and name of the file that is currently executing?


as others have said. You may also want to use os.path.realpath to eliminate symlinks:

import osos.path.realpath(__file__)


import inspect, osprint (inspect.getfile(inspect.currentframe())) # script filename (usually with path)print (os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))) # script directory

Update 2018-11-28:

Here is a summary of experiments with Python 2 and 3. With - runs - runs lib/
lib/ - prints filepath expressions

| Python | Run statement       | Filepath expression                    ||--------+---------------------+----------------------------------------||      2 | execfile            | os.path.abspath(inspect.stack()[0][1]) ||      2 | from lib import bar | __file__                               ||      3 | exec                | (wasn't able to obtain it)             ||      3 | import      | __file__                               |

For Python 2, it might be clearer to switch to packages so can use from lib import bar - just add empty files to the two folders.

For Python 3, execfile doesn't exist - the nearest alternative is exec(open(<filename>).read()), though this affects the stack frames. It's simplest to just use import foo and import - no files needed.

See also Difference between import and execfile

Original Answer:

Here is an experiment based on the answers in this thread - with Python 2.7.10 on Windows.

The stack-based ones are the only ones that seem to give reliable results. The last two have the shortest syntax, i.e. -

print os.path.abspath(inspect.stack()[0][1])                   # C:\filepaths\lib\bar.pyprint os.path.dirname(os.path.abspath(inspect.stack()[0][1]))  # C:\filepaths\lib

Here's to these being added to sys as functions! Credit to @Usagi and @pablog

Based on the following three files, and running from its folder with python (also tried execfiles with absolute paths and calling from a separate folder).

C:\filepaths\ execfile('')
C:\filepaths\ execfile('lib/')

import sysimport osimport inspectprint "Python " + sys.versionprintprint __file__                                        # main.pyprint sys.argv[0]                                     # main.pyprint inspect.stack()[0][1]                           # lib/bar.pyprint sys.path[0]                                     # C:\filepathsprintprint os.path.realpath(__file__)                      # C:\filepaths\main.pyprint os.path.abspath(__file__)                       # C:\filepaths\main.pyprint os.path.basename(__file__)                      # main.pyprint os.path.basename(os.path.realpath(sys.argv[0])) # main.pyprintprint sys.path[0]                                     # C:\filepathsprint os.path.abspath(os.path.split(sys.argv[0])[0])  # C:\filepathsprint os.path.dirname(os.path.abspath(__file__))      # C:\filepathsprint os.path.dirname(os.path.realpath(sys.argv[0]))  # C:\filepathsprint os.path.dirname(__file__)                       # (empty string)printprint inspect.getfile(inspect.currentframe())         # lib/bar.pyprint os.path.abspath(inspect.getfile(inspect.currentframe())) # C:\filepaths\lib\bar.pyprint os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # C:\filepaths\libprintprint os.path.abspath(inspect.stack()[0][1])          # C:\filepaths\lib\bar.pyprint os.path.dirname(os.path.abspath(inspect.stack()[0][1]))  # C:\filepaths\libprint