maybe a stupid question

Christian Heimes lists at cheimes.de
Sat Aug 9 02:19:15 CEST 2008


Strato wrote:
> I have an app installed in /usr/lib/python2.5/site-package/MyApp
> 
> I have a symlink in /usr/local/bin that points to 
> /usr/lib/python2.5/site-package/MyApp/myscript.py
> 
> Then, when I launch my script from anywhere using the symlink, how to 
> determine that the script is located in 
> /usr/lib/python2.5/site-package/MyApp ?


Put this in myscript.py:

import os.path
HERE = os.path.dirname(os.path.abspath(__file__))

You may also need

HERE = os.path.realpath(HERE)

Explanation:

Almost every module has a global variable __file__. It points to the 
path from which the module was loaded. os.path.abspath() gives you the 
absolute path to the module. os.path.dirname() strips away the file part 
and gives you the path to the directory. os.path.realpath() migt be 
required to eliminate all symbolic links.

Christian




More information about the Python-list mailing list