By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,036 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,036 IT Pros & Developers. It's quick & easy.

How to retrieve the filename of a module

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.