The folder a script is executed in

kyosohma at gmail.com kyosohma at gmail.com
Tue Aug 21 12:10:14 EDT 2007


On Aug 21, 10:23 am, Benjamin <musiccomposit... at gmail.com> wrote:
> On Aug 21, 4:10 am, aine_ca... at yahoo.com wrote:> Hi,
>
> > How do I find out what folder a script is in while it is executing?
>
> > For example, for the file "C:/folder/script.py" contain the following
> > two lines of code -
>
> > myLocation = GetMyLocation()
> > print myLocation
>
> def GetMyLocation():
>     runningFile = sys.argv[0] if __name__ == "__main__" else __file__
>     return os.path.dirname(runningFile)
>
>
>
> > >> C:/folder
>
> > Thanks,
>
> > Aine.

As I understand it, this is one portable way to do it:

path = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),
'.'))

Mike




More information about the Python-list mailing list