我在试着理解the docs。在
我的Python脚本生成的Python代码将在以后运行,因此我现在要检查我生成的代码是否有效。在
文件上说py_compile.compile(file[, cfile[, dfile[, doraise]]])
和
If doraise is true, a PyCompileError is raised when an error is encountered while compiling file
所以,我试过了
source = open(generatedScriptPath, 'rt').read() + '\n'
try:
import py_compile
x = py_compile.compile(source, '', '', True)
except py_compile.PyCompileError, e:
print str(e)
但内部异常永远不会命中,而是会捕获外部异常:
^{pr2}$我该怎么解决这个问题?请注意,我愿意接受其他选择,我只想问最简单的方法。”我刚才生成的代码在语法上是有效的Python吗?”在
阅读错误消息:“没有这样的文件或目录”。第一个参数应该是要打开的文件名。或者阅读文档:“源代码是从文件名/file/加载的”,您可能更喜欢“compile”内置代码,它可以编译一个Python字符串。在
相关问题 更多 >
编程相关推荐