473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

__file__

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__)

Apr 11 '07 #1
4 5084
En Tue, 10 Apr 2007 21:20:51 -0300, 7stud <bb**********@yahoo.com>
escribió:
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
[cut: other examples showing different paths to data.py]

__file__ corresponds to the filename used to locate and load the module,
whatever it is. When the module is found on the current directory
(corresponding to '' in sys.path), you get just the filename; if sys.path
contains a relative path, that's what you get; the same for any absolute
path.
Whatever path worked to find and load the module, that's stored as
__file__.

If you plan to use it, it's a good idea to make it early into an absolute
path (using os.path.abspath(__file__)) just in case the current directory
changes.
And some modules have __file__ in their __dict__ and some don't:
pprint.pprint(sys.__dict__)
sys is a builtin module: no sys.py/sys.pyc exists, so __file__ is
meaningless.

--
Gabriel Genellina

Apr 11 '07 #2
Hi,

Thanks for the response.

On Apr 11, 12:49 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>
__file__ corresponds to the filename used to locate and load the module,
whatever it is. When the module is found on the current directory
(corresponding to '' in sys.path), you get just the filename; if sys.path
contains a relative path, that's what you get; the same for any absolute
path.
Whatever path worked to find and load the module, that's stored as
__file__.

If you plan to use it, it's a good idea to make it early into an absolute
path (using os.path.abspath(__file__)) just in case the current directory
changes.
That last part doesn't seem to fit with your description above. What
does the current working directory have to do with the path that was
used to load a module? I would think the path that was used to load a
module is constant.

Apr 11 '07 #3
On Apr 11, 8:03 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
Hi,

Thanks for the response.

On Apr 11, 12:49 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
__file__ corresponds to the filename used to locate and load the module,
whatever it is. When the module is found on the current directory
(corresponding to '' in sys.path), you get just the filename; if sys.path
contains a relative path, that's what you get; the same for any absolute
path.
Whatever path worked to find and load the module, that's stored as
__file__.
If you plan to use it, it's a good idea to make it early into an absolute
path (using os.path.abspath(__file__)) just in case the current directory
changes.

That last part doesn't seem to fit with your description above. What
does the current working directory have to do with the path that was
used to load a module? I would think the path that was used to load a
module is constant.
You are correct, but that is not what GG was talking about. Here is an
example of what he meant:

While your cwd is /bar, you load module foo from a *relative*( path,
e.g. ./foo.py. If you do the os.path.abspath("./foo.py") immediately
as recommended, you get the correct answer: /bar/foo.py. However if
you change your cwd to /zot, then do the os.path.abspath("./foo.py"),
you get /zot/foo.py which is a nonsense.

HTH,
John

Apr 11 '07 #4
On Apr 11, 6:55 am, "John Machin" <sjmac...@lexicon.netwrote:
On Apr 11, 8:03 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
Hi,
Thanks for the response.
On Apr 11, 12:49 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
__file__ corresponds to the filename used to locate and load the module,
whatever it is. When the module is found on the current directory
(corresponding to '' in sys.path), you get just the filename; if sys.path
contains a relative path, that's what you get; the same for any absolute
path.
Whatever path worked to find and load the module, that's stored as
__file__.
If you plan to use it, it's a good idea to make it early into an absolute
path (using os.path.abspath(__file__)) just in case the current directory
changes.
That last part doesn't seem to fit with your description above. What
does the current working directory have to do with the path that was
used to load a module? I would think the path that was used to load a
module is constant.

You are correct, but that is not what GG was talking about. Here is an
example of what he meant:

While your cwd is /bar, you load module foo from a *relative*( path,
e.g. ./foo.py. If you do the os.path.abspath("./foo.py") immediately
as recommended, you get the correct answer: /bar/foo.py. However if
you change your cwd to /zot, then do the os.path.abspath("./foo.py"),
you get /zot/foo.py which is a nonsense.

HTH,
John
Thanks for all the help.

Apr 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris Pomasl | last post by:
We picked up a helpdesk script from sourceforge, Mantis. This script uses the __FILE__ magic constant to ostensibly derive the path to the include directory from a script in the top level...
1
by: Chris Pomasl | last post by:
Does no one have any insight?......please? Chris -------- Original Message -------- Subject: Problem with __FILE__ magic constant Date: Tue, 06 Jan 2004 16:43:41 GMT From: Chris Pomasl...
6
by: Tom | last post by:
I'm trying to dynamically adjust the path for one of my scripts using this: $script_path = str_replace( $_SERVER, "", dirname(realpath(__FILE__)) ) . DIRECTORY_SEPARATOR; The problem: the...
1
by: Spry | last post by:
Hi, I wanted to write macros for finding the number of memory allocations and deallocations also wanted to find the locations. The code I have is a pretty big one. I have a wrapper on top of...
5
by: jake1138 | last post by:
I couldn't find an example of this anywhere so I post it in the hope that someone finds it useful. I believe this is compiler specific (I'm using gcc), as C99 defines __VA_ARGS__. Comments are...
5
by: baumann.Pan | last post by:
where are these macros defined? can I use it a release ver code?
2
by: fortepianissimo | last post by:
This is a question to all of you who use Komodo IDE for development: when I tried to debug my script which uses __file__ to get the absolute path to the file, Komodo complained that the variable is...
18
by: Lukas Ruf | last post by:
Dear all, for debugging purposes, I like the pre-compiler macros __FILE__, __FUNCTION__, __LINE__ I have been wondering if there is a short form of __FILE__ that provides only the filename...
3
by: mh | last post by:
So on most modules I import, I can access the .__file__ attribute to find the implementation. ie: >>> import time >>> time.__file__ '/data1/virtualpython/lib/python2.3/lib-dynload/timemodule.so'...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.