简体   繁体   中英

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.

server.py list all sevrer name in text.log file and this log file is used by test.py as a input.

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.

So far i have tried with execfile("server.py") >text.log in test.py which didn't worked.

Use subprocess.call with stdout argument:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM