RV-user
RV-user

Reputation: 13

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.

Upvotes: 1

Views: 1749

Answers (1)

falsetru
falsetru

Reputation: 369344

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

Upvotes: 4

Related Questions