PyCompileError exception

流过昼夜 提交于 2019-12-24 18:47:41

问题


Write a function to check if an input file contain syntactically correct Python code, if the input file does not contain syntactcally correct Pyhton code the function should print the following message:

<filename> does not contain syntactically correct Python code

where <filename> is the name of the input file . We suggest importing the py_compile module and using the compile function (with the parameter doraise = True) and the PyCompileError exception of this module.

I wrote this function, is it correct?:

def check(python_file):
    try:
        file = open(python_file, 'r')
        py_compile.compile(python_file, doraise=True)

    except py_compile.PyCompileError:
       print("<"+python_file+"> does not contain syntactically correct Python code")

来源:https://stackoverflow.com/questions/49554798/pycompileerror-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!