我使用以下代码将print语句重定向到文本文件中。你知道吗
old_stdout = sys.stdout
log_file = open("message.log","w")
sys.stdout = log_file
print "this will be written to message.log"
subprocess.call("iPython.exe", "script.py") #subprocesses here take this form
sys.stdout = old_stdout
log_file.close()
我的问题是,这似乎不适用于子流程。“中的打印语句”脚本.py“不会出现在”消息.log". 我怎样才能让他们这么做?你知道吗
使用
subprocess.Popen
而不是subprocess.call
,这样可以重定向stdout
和stderr
。你知道吗您还可以像这样将stderr重定向到stdout,以便将来自
script.py
的所有输出发送到单个文件。你知道吗然而,仅仅调用
iPython
来加载另一个脚本的子进程是一种糟糕的工作方式。相反,您应该直接调用script.py
模块。你知道吗相关问题 更多 >
编程相关推荐