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(d ata.__dict__)
print
print "*******"
print
pprint.pprint(s ys.__dict__) 4 5114
En Tue, 10 Apr 2007 21:20:51 -0300, 7stud <bb**********@y ahoo.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(s ys.__dict__)
sys is a builtin module: no sys.py/sys.pyc exists, so __file__ is
meaningless.
--
Gabriel Genellina
Hi,
Thanks for the response.
On Apr 11, 12:49 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
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.
On Apr 11, 8:03 pm, "7stud" <bbxx789_0...@y ahoo.comwrote:
Hi,
Thanks for the response.
On Apr 11, 12:49 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
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
On Apr 11, 6:55 am, "John Machin" <sjmac...@lexic on.netwrote:
On Apr 11, 8:03 pm, "7stud" <bbxx789_0...@y ahoo.comwrote:
Hi,
Thanks for the response.
On Apr 11, 12:49 am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 directory.
Reading the doc about __FILE__ did not gain me enough insight as to what
is going on.
My testbed is running PHP 4.1.0 but the server where it is to run is
4.0.6. The script specifically says that it is designed for a minimum
4.0.6.
|
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 <pomaslNOSPAM@starband.net>
Organization: Starband Communications
Newsgroups: comp.lang.php
|
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 server I'm currently testing it on returns for
$_SERVER something like the path "home/user/path/" and
for __FILE__ "home2/user/path/scriptdir/" I tried to trace the
discrepancy running phpinfo, but the only reference I can find to
|
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 malloc(int
x) as Xmalloc(int x)
Now I want to write a macro for Xmalloc which can log the location of the
file and the line and the number of bytes allocated.
|
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 welcome.
This will print the file name and line number followed by a format
string and a variable number of arguments. The key here is that you
MUST have a space between __LINE__ and the last comma, otherwise
__LINE__ gets eaten by the ## if...
| |
by: baumann.Pan |
last post by:
where are these macros defined? can I use it a release ver code?
|
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 not defined.
Anyway to work around this? (without changing the code)
Or if I need to change the code, how do I achieve the same thing I
wanted? Thanks in advance!
|
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 without path.
I am using gcc as available in Debian unstable:
|
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'
>>> import socket
>>> socket.__file__
'/data1/virtualpython/lib/python2.3/socket.pyc'
This doesn't work on the "thread" module:
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |