print line number and source filename

Peng Yu pengyu.ut at gmail.com
Tue Jun 22 12:44:00 EDT 2010


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



More information about the Python-list mailing list