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


I have scripts calling other script files but I need to get the filepath of the file that is currently running within the process.

For example, let's say I have three files. Using execfile:

  • calls
  • In turn, calls

How can I get the file name and path of, from code within, without having to pass that information as arguments from

(Executing os.getcwd() returns the original starting script's filepath not the current file's.)

6/15/2015 4:06:52 PM

Accepted Answer


import inspect, os
print inspect.getfile(inspect.currentframe()) # script filename (usually with path)
print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory
8/25/2012 9:57:31 PM


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

import os


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow