The file executing

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Jul 9 13:42:22 CEST 2007


En Fri, 06 Jul 2007 17:15:22 -0300, Benjamin <musiccomposition at gmail.com>  
escribió:

>> > > > How does one get the path to the file currently executing (not the
>> > > > cwd). Thank you
> So:
> if __name__ == "main":
>     currentDir = os.path.dirname(sys.argv[0])
> else:
>     currentDir = os.path.dirname(__file__)

I think you meant to test for "__main__", but anyway, this should be  
enough:
currentDir = os.path.dirname(__file__)
or perhaps os.path.dirname(os.path.abspath(__file__)). Try to determine  
that early in your program because __file__ may contain a relative path  
(and will give a wrong result after changing the current directory).

-- 
Gabriel Genellina




More information about the Python-list mailing list