print line number and source filename

Shashwat Anand anand.shashwat at gmail.com
Tue Jun 22 12:57:12 EDT 2010


22:26:51 l0nwlf-MBP:~/Desktop$ cat test.py
print __file__

22:26:55 l0nwlf-MBP:~/Desktop$ python test.py
test.py


On Tue, Jun 22, 2010 at 10:26 PM, Shashwat Anand
<anand.shashwat at gmail.com>wrote:

> you can use __file__
>
>
> On Tue, Jun 22, 2010 at 10:14 PM, Peng Yu <pengyu.ut at gmail.com> wrote:
>
>> I want to print filename and line number for debugging purpose. So far
>> I only find how to print the line number but not how to print
>> filename.
>>
>> import inspect
>> print inspect.currentframe().f_lineno
>>
>> I found inspect.getsourcefile(), but I have to supply a class name to
>> it. I have searched online, but I don't find how to print the source
>> filename. Would you please let me know?
>>
>> Also, always importing the inspect module and getting the frame and
>> accessing the lineno from the frame is not very convenient to type. Is
>> there a shorter way to access the line number (for example, in C++ and
>> perl, __LINE__ can be used to access line number, which is much more
>> convenient than the way that I found in python).
>>
>> --
>> Regards,
>> Peng
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100622/8a718303/attachment-0001.html>


More information about the Python-list mailing list