replacement for __file__ in compiled exe

TechieInsights GDoermann at gmail.com
Mon Jan 5 18:00:34 EST 2009


Yes, that is my exact question.  How do you get the file path of your
script/program.  Normally in python you can use __file__ and it will
return the file path of the script you are in, however, when you
compile the script to exe py2exe (or whatever util you are using)
compresses them into a zip folder... not usually a problem, except
when you want the access the file path of the exe.

Thanks for the answer... I should have thought of that all along...
just look on the system path.  Sometimes the answer was right in front
of your face the whole time...

Greg



On Jan 5, 1:51 pm, John Machin <sjmac... at lexicon.net> wrote:
> On Jan 6, 7:03 am, TechieInsights <GDoerm... at gmail.com> wrote:
>
> > __file__ command does not work when compiled to exe.  It makes since
> > because the file is now in a compressed library.  Is there a
> > replacement or something else you can do?  The real problem is that
> > when you create an exe of your program with python embedded, you can't
> > always guarantee that your current directory is the directory of your
> > program.
>
> How can you *ever* guarantee that the current directory is the same as
> the directory in which the program resides? This lack of guarantee is
> quite independent of whether the "program" is .py, .exe, .bat, .com,
> etc. Isn't the real problem how to find out which directory the
> program is in?
>
> >  I guess when you could just set a registry entry on
> > windows... but it would be nice to have a quick fix for this (like
> > os.path.dirname(__file__)).
>
>     if hasattr(sys, 'frozen'):
>         answer = os.path.split(sys.executable)[0]
>     else:
>         answer = os.path.split(sys.argv[0])[0]
>
> (maybe) ... your question is a little unclear.
>
> HTH,
> John




More information about the Python-list mailing list