468,288 Members | 1,978 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,288 developers. It's quick & easy.

How to retrieve the filename of a module

mku
Hi,

there´s a function inside a module. How can these function retrieve
the path+name of his module ? (The path is most important).
That should also work if the module is part of a package.

Thanks in advance

Martin

Oct 20 '05 #1
3 3143
Il 2005-10-20, mku <ma**********@thomson.net> ha scritto:
Hi,

there´s a function inside a module. How can these function retrieve
the path+name of his module ? (The path is most important).
That should also work if the module is part of a package.

rhymes@voodoo:~ $ cat > test.py
print __file__
import os
print os.path.abspath(__file__)

rhymes@voodoo:~ $ python test.py
test.py
/home/rhymes/test.py

Bye

--
Lawrence
http://www.oluyede.org/blog
Oct 20 '05 #2
mku wrote:
Hi,

there´s a function inside a module. How can these function retrieve
the path+name of his module ? (The path is most important).
That should also work if the module is part of a package.

Thanks in advance

Martin


Try the following in the function:

import traceback
f = traceback.extract_stack(limit=2)

If you output f to the interpreter, you'll see the filename but I don't
know what position in the output list it is guaranteed to be.

Jim
Oct 20 '05 #3
Il 2005-10-20, Lawrence Oluyede <ra***@dot.com> ha scritto:
Il 2005-10-20, mku <ma**********@thomson.net> ha scritto:
Hi,

there´s a function inside a module. How can these function retrieve
the path+name of his module ? (The path is most important).
That should also work if the module is part of a package.

rhymes@voodoo:~ $ cat > test.py
print __file__
import os
print os.path.abspath(__file__)

rhymes@voodoo:~ $ python test.py
test.py
/home/rhymes/test.py


Also __name__ for the name of the module

--
Lawrence
http://www.oluyede.org/blog
Oct 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Didier FRAISSE | last post: by
17 posts views Thread by Sean Ross | last post: by
reply views Thread by Tero Saarni | last post: by
1 post views Thread by Eugfene | last post: by
2 posts views Thread by Rob Cowie | last post: by
6 posts views Thread by mphanke | last post: by
reply views Thread by Ron Adam | last post: by
2 posts views Thread by Adam Teale | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.