[Tkinter-discuss] Help with directories.

Michael Lange klappnase at web.de
Mon Jan 10 21:05:58 CET 2005


On Mon, 10 Jan 2005 16:22:13 -0300
"Jorge Constanzo" <lumberjack at educarchile.cl> wrote:

> Hello:
> 
> My problem is this: I made a program that it reads from a file.
> This file are in the same directory with the program. For getting this path, I've the
> following code:
> 
> import os
> path_file=os.path.dirname(__file__)
> 
> The command os.path.dirname(path_dir) returns the parent path of the path_dir and
> __file__ returns the path of the current file (the file of the code). This code
> worked very well in Windows (any version), but in Linux it doesn't work. I knew that
> the command __file__ doesn't work in Linux (by means of print command :D) and that is
> the reason of the problem.
> 
> My question is: somebody knows if it exists a command what does the same than command
> __file__, but in Linux?
> 

I think only modules that are imported by your main program file have the __file__ attribute;
at least it works this way on my linux box:

>>> import Tix
>>> print Tix.__file__
/usr/lib/python2.3/lib-tk/Tix.pyc

if you want to know the directory of your main program file you can do:

path_file = sys.path[0]

The location of the main program file is always inserted into sys.path so you can easily import
modules from within the same directory.

I hope this helps

Michael





More information about the Tkinter-discuss mailing list