views:

104

answers:

2

Hello everyone,

How can I get the file name and line number in python script.

Exactly the file information we get from an exception traceback. In this case without raising an exception.

+2  A: 
inspect.currentframe().f_back.f_lineno

From ActiveState Code.

1st result for Google: python line number

mcandre
+1  A: 

Thanks mcandre. The answer is:

from inspect import currentframe, getframeinfo

frameinfo = getframeinfo(currentframe())

print frameinfo.filename, frameinfo.lineno
Joey