【发布时间】:2015-08-27 19:35:53
【问题描述】:
有没有办法通过执行文件来获取错误的行号?假设我有以下代码:
exec(open("test.py").read())
并且文件有错误,其中IDLE弹出如下:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
return self.func(*args)
File "C:\Users\henrydavidzhu\Desktop\Arshi\arshi.py", line 349, in runFile
exec(open(self.fileName).read())
File "<string>", line 2, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
我想知道错误语句 (TypeError: unsupported operand type(s) for +: 'int' and 'str') 和行号。这不是重复的,因为我使用的是Python 3,而不是Python 2。
【问题讨论】:
-
您的意思是您想以编程方式知道行号?
-
@MartijnPieters 完全正确。
-
附带问题:你为什么不使用
import? -
@ReutSharabani 你到底是什么意思?
-
你有一些 python 代码并且你正在使用
exec来执行它,为什么不使用import来代替呢?
标签: python python-3.x