Python 脚本的文件名和行号 [英] filename and line number of Python script

查看:30
本文介绍了Python 脚本的文件名和行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Python 脚本中获取文件名和行号?

How can I get the file name and line number in a Python script?

正是我们从异常回溯中获得的文件信息.在这种情况下不会引发异常.

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

推荐答案

感谢 mcandre,答案是:

Thanks to mcandre, the answer is:

#python3
from inspect import currentframe, getframeinfo

frameinfo = getframeinfo(currentframe())

print(frameinfo.filename, frameinfo.lineno)

这篇关于Python 脚本的文件名和行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆