跳转至

Python 获取当前行数

概要: 使用标准库inspect,获取Python脚本运行的行数

创建时间: 2022.08.24 00:00:42

更新时间: 2022.11.09 23:09:54

源码

Python
1
2
3
4
5
6
7
8
import inspect


def __line__():
    return inspect.currentframe().f_back.f_lineno


print(f'Running file: {__file__}, line: {__line__()}')

输出

Text Only
Running file: /Users/lzwang/PycharmProjects/PyPlayground/temp/test1.py, line: 7