Retrieve source filename

Thomas Heller theller at python.net
Thu Jul 31 14:00:23 EDT 2003


Irmen de Jong <irmen at -NOSPAM-REMOVETHIS-xs4all.nl> writes:

> Shrom wrote:
>
>> I know that you can retrieve main file name with sys.argv[0] and file name
>> for a module with modulename.__file__ but is there a common way to retrieve
>> file name for both main program and modules as the __FILE__ constant in PHP
>> ?
>
> I don't know if it's the "right" way, but this works:
>
> import inspect
> print inspect.getfile(inspect.currentframe())
>
This one should also work:

if __name__ == "__main__":
    import sys
    __file__ = sys.argv[0]

Thomas




More information about the Python-list mailing list