我试过两年前发布的代码:
import subprocess
with open("output.txt", "wb") as f:
subprocess.check_call(["python", "file.py"], stdout=f)
import sys
import os.path
orig = sys.stdout
with open(os.path.join("dir", "output.txt"), "wb") as f:
sys.stdout = f
try:
execfile("file.py", {})
finally:
sys.stdout = orig
它挂起终端直到I ctl-z,然后它使终端崩溃,但打印输出。 我是新的编码和不知道如何解决。我显然做错了什么。谢谢你的帮助。你知道吗
只需使用
write
打开并写入文件即可。你知道吗由于您对编码还不熟悉,我只想告诉您,使用
with
打开文件实际上会在运行缩进代码后自动关闭它。祝你的项目好运!你知道吗相关问题 更多 >
编程相关推荐