[英]How to execute python script from another python script and send it's output in text file
I have a script test.py which is used for server automation task and have other script server.py which list out all server name. 我有一个脚本test.py用于服务器自动化任务,还有另一个脚本server.py列出了所有服务器名称。
server.py list all sevrer name in text.log file and this log file is used by test.py as a input. server.py在text.log文件中列出所有服务商名称,而test.py将此日志文件用作输入。
I want one single script test.py to execute server.py from inside it and also redirect server.py script output to text.log file as well. 我想要一个脚本test.py从其内部执行server.py,还将server.py脚本输出也重定向到text.log文件。
So far i have tried with execfile("server.py") >text.log in test.py which didn't worked. 到目前为止,我已经在test.py中使用execfile(“ server.py”)> text.log尝试了,但没有成功。
Use subprocess.call
with stdout
argument: 将subprocess.call
与stdout
参数一起使用:
import subprocess
import sys
with open('text.log', 'w') as f:
subprocess.call([sys.executable, 'server.py'], stdout=f)
# ADD stderr=subprocess.STDOUT if you want also catch standard error output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.