7
7stud
Hi,
I'm having trouble understanding what the definition of __file__ is.
With this program:
------
#data.py:
def show():
print __file__
if __name__ == "__main__":
show()
-------
if I run data.py with the prompt pointing to the directory that
contains data.py, then __file__ produces a filename:
data.py
If I run data.py with the prompt pointing to a different directory,
then file produces what I entered on the command line, e.g.:
../2testing/dir1/data.py
If I import the data module into another python program, e.g.:
-------
#test1.py:
from data import show
show()
------
and test1.py is in the same directory as data.py, __file__ produces an
absolute path to the data module:
/Users/me/2testing/dir1/data.pyc
If test1.py is in a different directory than data.py, __file__
produces the path used in sys.path.append(), e.g.:
----
import sys
sys.path.append("./2testing/dir1")
import data
data.show()
---output:------
../2testing/dir1/data.pyc
====or======
import sys
sys.path.append("/Users/me/2testing/dir1")
import data
data.show()
---output:-------
/Users/me/2testing/dir1/data.pyc
And some modules have __file__ in their __dict__ and some don't:
-------
import sys, pprint, data
pprint.pprint(data.__dict__)
print
print "*******"
print
pprint.pprint(sys.__dict__)
I'm having trouble understanding what the definition of __file__ is.
With this program:
------
#data.py:
def show():
print __file__
if __name__ == "__main__":
show()
-------
if I run data.py with the prompt pointing to the directory that
contains data.py, then __file__ produces a filename:
data.py
If I run data.py with the prompt pointing to a different directory,
then file produces what I entered on the command line, e.g.:
../2testing/dir1/data.py
If I import the data module into another python program, e.g.:
-------
#test1.py:
from data import show
show()
------
and test1.py is in the same directory as data.py, __file__ produces an
absolute path to the data module:
/Users/me/2testing/dir1/data.pyc
If test1.py is in a different directory than data.py, __file__
produces the path used in sys.path.append(), e.g.:
----
import sys
sys.path.append("./2testing/dir1")
import data
data.show()
---output:------
../2testing/dir1/data.pyc
====or======
import sys
sys.path.append("/Users/me/2testing/dir1")
import data
data.show()
---output:-------
/Users/me/2testing/dir1/data.pyc
And some modules have __file__ in their __dict__ and some don't:
-------
import sys, pprint, data
pprint.pprint(data.__dict__)
print "*******"
pprint.pprint(sys.__dict__)