module and file locations

Michele Simionato mis6 at pitt.edu
Sat Jul 5 08:55:46 EDT 2003


Stuart Galloway <stuart at gallpath.com> wrote in message news:<xtVMa.720$JI4.10080 at news-server.bigpond.net.au>...
> Hi,
> 
> I am writing a debugging a Tkinter program.
> 
> While debugging I want to run it by pressing F5.
> When in use I want to run it by double-clicking (win2000)
> 
> The program reads and write to various files which I want to store in 
> the same directory (or a sub directory) of the main script.
> 
> My question is: How can my program where it lives so I can get the base 
> directory?
> argv[0]  - will this work debugging if I just import the file?
> There is some about finding where a module is.  This seems to return 
> readable text that would require parsing rather than just a file name
> 
> Thanks,
> 
> Stuart

The global variable __file__ contains the file name of the current module. 
For instance

#put this in a file called x.py
print __file__ 

returns 'x.py' when you do

$ python x.py

For some strange reason it does not work if I execute x.py from IDLE 1.0b2
on a Windows 98SE machine. Is it working for you?  

                                                          Michele




More information about the Python-list mailing list