[英]How to execute python script from another python script and send it's output in text file
我有一個腳本test.py用於服務器自動化任務,還有另一個腳本server.py列出了所有服務器名稱。
server.py在text.log文件中列出所有服務商名稱,而test.py將此日志文件用作輸入。
我想要一個腳本test.py從其內部執行server.py,還將server.py腳本輸出也重定向到text.log文件。
到目前為止,我已經在test.py中使用execfile(“ server.py”)> text.log嘗試了,但沒有成功。
將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.