how to get file name of the running .py file

Fredrik Lundh fredrik at pythonware.com
Thu Aug 24 02:26:55 EDT 2006


Larry Bates wrote:

>> How to get the name of the running .py file like the macro _FILE_ in C?

> import os
> import sys
> print sys.argv[0]
> 
> or if you just want the script and not the full path
> 
> print os.path.basename(sys.argv[0])

except that __FILE__ is the name of the current source file, not the 
name of the main program.

the global name __file__ is a better choice; when definied, it points to 
the file from which the current module was loaded (this might be a byte 
code file or a shared library).

also see:

     http://pyref.infogami.com/__file__

</F>




More information about the Python-list mailing list