py2exe windows apps path question

vincent wehren vincent at visualtrans.de
Tue Aug 2 20:21:29 CEST 2005


"Gregory Piñero" <gregpinero at gmail.com> schrieb im Newsbeitrag 
news:mailman.2632.1123005673.10512.python-list at python.org...

|And here is how I make sure I'm always using the right directory in my 
scripts:
|
|Put this code at the top:
|import sys
|curdir=os.path.dirname(sys.argv[0])
|#print curdir
|Then I use curdir to build all of the paths in my app:
|For example let's get a list of files in a folder:
|lstresumes=os.listdir(os.path.join(curdir,resume_folder_path)) #get
|list of resumes

<snipped>

Greg,

If you need something that works both on a frozen app as well as an 
(unfrozen) python
script, you'd be better off using something like:

def getAppPrefix():
    """Return the location the app is running from
    """
    isFrozen = False
    try:
        isFrozen = sys.frozen
    except AttributeError:
        pass
    if isFrozen:
        appPrefix = os.path.split(sys.executable)[0]
    else:
        appPrefix = os.path.split(os.path.abspath(sys.argv[0]))[0]
    return appPrefix

Now you can use the return value of getAppPrefix() everywhere you need to 
calculate paths relative to your app, regardless if it involves a regular 
script or py2exe'ified one.

Regards,

--

Vincent Wehren





More information about the Python-list mailing list