Get Path of current Script

Mario Rol mario.rol at gmail.com
Tue Mar 15 09:47:17 EDT 2011


On Mar 14, 10:25 am, Alexander Schatten <asch... at gmail.com> wrote:
> Hi,
>
> could someone help me with a small problem? I wrote a Python script
> that does some RegEx... transformations. Now, this script loads some
> configuration data from a file located in the same directory:
>
> open ('config.txt', 'r').
>
> However, this only works when I execute the script being in the
> directory where the script is locates, because otherwise, of course,
> this config file is not found, as the path is relative. Now my
> question: is there an easy way (API) to get the directory of the
> currently running script? Something along the line of:
>
> open (API.getCurrentPath + 'config.txt', 'r').
>
> thanks a lot,
>
> cheers
>
> Alex

os.path.join(sys.path[0], 'config.txt')

If the script and config.txt are in /tmp this will return '/tmp/
config.txt' no matter from which directory you started the script.

Mario



More information about the Python-list mailing list