How to get path of a .py script

Manuel M. Garcia mgarcia at cole-switches.com
Wed Dec 11 13:15:53 EST 2002


On 11 Dec 2002 04:15:43 -0800, devrim at machsim.com (Devrim Erdem)
wrote:
(edit)
>How can I get the path of the script file itself so that I can locate
>files which are relative to the script file ?

If you want to know the path of the script also when it is imported by
another script:

import os
import sys

if __name__ == '__main__':
    p = sys.argv[0]
else:
    p = __file__

print os.path.split(os.path.realpath(p))

~~~

Manuel



More information about the Python-list mailing list