sample.py 是现有的 python 文件。
运行该 python 文件的命令:
>>> python sample.py
错误信息:
SyntaxError: invalid syntax
如何在 Python 2.7.14 shell 中创建和运行 python 文件。
最佳答案
这可以使用 exec
函数来完成。假设 sample.py
有一个 print "hello world"
语句:
示例
>>> file = open('sample.py','r')
>>> exec(file.read())
hello world
您可以在此处阅读有关 exec()
以及如何使用它的更多信息:https://docs.python.org/3/library/functions.html#exec
关于python - 创建并运行 python 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48251646/