finding the file of a module from inside a class

Duncan Booth duncan at NOSPAMrcp.co.uk
Tue Sep 25 05:54:49 EDT 2001


"Thomas Weholt" <thomas at gatsoft.no> wrote in
news:YGXr7.371$n5b.170335232 at news.telia.no: 

> say I got a module test.py with this content:
> 
> class MyClass:
>     def __init__(self):
>         pass
>     def myfile(self):
>         return '' # ????
    return os.path.abspath(__file__)
> 
> if I put this into a folder, ex. /home/thomas/dev/test/, how can I get
> information about what file the code instance actually is stored in,
> from inside my class? I want the myfile-method to return
> /home/thomas/dev/test/test.py ( if the class is stored in a module
> called test.py in a folder /home/thomas/dev/test/ of course ).

__file__ should give you what you need. It may give you a relative path, 
but you can use os.path.abspath() to make it absolute.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list