473,396 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3400
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Didier FRAISSE | last post by:
i need to retrieve every day an email and to save his attachments for later processing. i found some piece of code in the documentation like import poplib pop = poplib.POP3(Host)...
17
by: Sean Ross | last post by:
Hi. Recently I made a small script to do some file transferring (among other things). I wanted to monitor the progress of the file transfer, so I needed to know the size of the files I was...
0
by: Tero Saarni | last post by:
I'm using Logging in my library module for producing trace log of certain events. For being more useful these log entries should be associated with the filename and line number of *users* code...
1
by: Eugfene | last post by:
I have the following function in a html file: function selectEdit(fileID, processCode, processID, fileName,fileDesc) { document.forms.recordID.value = fileID; document.forms.processor.value =...
2
by: Rob Cowie | last post by:
Hi, Given a string representing the path to a file, what is the best way to get at the filename? Does the OS module provide a function to parse the path? or is it acceptable to split the string...
6
by: mphanke | last post by:
Hi, how can I retrieve the icon for a registered file extension from the registry and show it in my ListView? Any hints appreciated, Martin
6
by: Allen | last post by:
Hi, I want to retrieve the selected folder and entered file name from the SaveFilsDialog. The user is able to create or selected a different folder during the dialog displayed. If user enters...
0
by: Ron Adam | last post by:
Is there a function to find a filename from a dotted module (or package) name without importing it? The imp function find_module() doesn't work with dotted file names. And it looks like it may...
2
by: Adam Teale | last post by:
hey guys Is there a builtin/standard install method in python for retrieving or finding out an image's dimensions? A quick google found me this:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.