如何使用Python语言检查文件是否为Python文件 由 惊魂者发布于 2020-05-23 23:11:04 我正在开发一个Web应用程序,应聘者提交python代码,平台将其通过招聘者编写的单元测试传递。 我正在使用Django,但无法验证单元测试文件。 如何测试文件是否为python文件且格式正确(无语法或词法错误)?使用Python吗? 谢谢
You can use the built-in function
compile
. Pass the content of the file as a string as the first param, file from which the code was read (If it wasn't read from a file, you can give a name yourself) as the second param andmode
as the third.尝试:
如果测试文件的内容是:
将显示以下消息: