Get Path of current Script

Gennadiy Zlobin gennad.zlobin at gmail.com
Mon Mar 14 05:42:58 EDT 2011


To get the directory of the current running script try:

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



- Gennadiy <gennad.zlobin at gmail.com>


On Mon, Mar 14, 2011 at 3:25 PM, Alexander Schatten <aschatt 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110314/d2a60659/attachment-0001.html>


More information about the Python-list mailing list