how to get file name of the running .py file

Bill Pursell bill.pursell at gmail.com
Thu Aug 24 00:29:32 EDT 2006


Jason Jiang wrote:
> Hi,
>
> How to get the name of the running .py file like the macro _FILE_ in C?

There are many ways--IMO the easiest is with __file__:

>>> print __file__
/home/bill/.pystart
>>>
[tmp]$ cat foo.py
#!/usr/bin/env python

print "The name of the file is:%s"%__file__
[tmp]$ ./foo.py
The name of the file is:./foo.py




More information about the Python-list mailing list