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.
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.
inspect.currentframe().f_back.f_lineno
From ActiveState Code.
1st result for Google: python line number
Thanks mcandre. The answer is:
from inspect import currentframe, getframeinfo
frameinfo = getframeinfo(currentframe())
print frameinfo.filename, frameinfo.lineno