Return a String of the Path the Running Python Program Resides In


#1

Anyone know if there is a rs method that returns the path name of the currently executing python script?

I have data in subfolders of my python code folder that I’d like to access. But I don’t want to code an absolute path to that folder.

Thoughts anyone?

Thanks,
Mark


#2

Hi Mark,

not shure if this runs on mac but it worked here to get the path with file name under win7:

import os
p = os.path.realpath(__file__) 
print(p)

and this one to strip only the path without filename:

import os
print os.path.dirname(os.path.realpath(__file__))

c.


#3

Thanks. Doing os.path.realpath("./") worked nicely.

Mark