[Pythonmac-SIG] Re: Newbie path problem
Russell E. Owen
rowen at cesmail.net
Wed Feb 25 13:51:34 EST 2004
In article
<6.0.2.0.0.20040225102754.02c3e180 at pop.promibra.intra.mrw.wallonie.be>,
LALOUX Martin <M.Laloux at mrw.wallonie.be> wrote:
> I am new in Macpython (Panther, working in win32 python) and i have a
> problem with paths. When I launch a script (doubleclicking the .py file ->
> PythonLauncher) which needs to open a file located in the same directory, I
> have a message which says "file" not found.
> When i launch the same script from terminal (cd..directory), no problem....
__file__ contains the location of the current file/module, and in
general <module>.__file__ contains the path to any <module> you
imported, e.g. os.__file__.
So..., if you set filename to name of the file you want to locate:
mydir = os.path.dirname(__file__)
filepath = os.path.join(mydir, filename)
I personally use this trick to find resources (e.g. help files), e.g.
put help files in a package called Help, then find them via:
import Help
helpdir = os.path.dirname(Help.__file__)
It works very well. However, a minor warning: if you plan to ever turn
your script into a normal double-clickable Mac application, you'll find
that job easier if you avoid mixing python and non-python code (due to
limitations in bundlbuilder, the module that builds Mac apps).
-- Russell
More information about the Pythonmac-SIG
mailing list