1

I have two python scripts: script1.py and script2.py

I want to run script1 from script2 (os.system, subprocess.Popen, ..) and then log script1's output (stdout, stderr) to a file log.txt, while still seeing script1's and script2's outputs on my terminal (as I would see, without the logging feature), as the are printed on ..

N.B.: It essential that log.txt exactly reflects the output (stdout, stderr) i would get on screen/terminal, corresponding to script1's run.

Thanks in advance for your kind help.

dOpP

2 Answers 2

1

Currently there is no function in python standard library that does this but you can use tendo.tee().

Sign up to request clarification or add additional context in comments.

1 Comment

@dOpP I didn't had time to write a documentation for the library, but if it is needed file a bug, maybe this will motivate me to write the doc for it.
1

Use the subprocess module (not os.system). Send stdout and stderr to a pipe and capture them in local variables (using communicate probably). Once you have them in local vars you can both print them and write them to a file (so you're probably most of the way there already).

Something like:

log = open("log.txt", "w+")
p = subprocess.Popen(['/usr/bin/env', 'python', 'script1.py'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = p.communicate()
print out,err
log.write(out)
log.write(err)

1 Comment

Hello guys, Once more, thanks to every one who answered me (sorin, nick b., etc.).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.