Retrieve source filename
Steven Taschuk
staschuk at telusplanet.net
Thu Jul 31 21:02:39 EDT 2003
Quoth Shrom:
> 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
> ?
As of 2.3, the __main__ module also has a __file__ attribute, so
you can just do
sys.modules[__name__].__file__
The (an?) exception is when __main__ is the text in a 'python -c'
command -- then there is, of course, no file name. And for
compatibility with versions < 2.3, you have to check for the case
__name__ == '__main__' and go to sys.argv[0].
--
Steven Taschuk 7\ 7'Z {&~ .
staschuk at telusplanet.net Y r --/hG-
(__/ )_ 1^1`
More information about the Python-list
mailing list