2

I want to run an external program from python, redirect output (lots of text) to a log file and wait for that program to finish. I know I can do it via bash:

#! /bin/bash
my_external_program > log_file 2>&1
echo "done"

But how can I do the same with python? Note that with the bash command, I can check the log_file while the program is running. I want this property in python as well.

2 Answers 2

2

See the subprocess module.

For example:

with open("log_file", "w") as log_file:
    subprocess.run(["my_external_program"], stdout=log_file, stderr=log_file)
print("done")
Sign up to request clarification or add additional context in comments.

2 Comments

+1. great solution with python subprocess, but I choose @Marco solution since it is shorter (though os.system is said to be deprecated).
The, main, problem with os.system() is security. It is also, now, redundant with subprocess.run('...', shell=True). The string passed to it is passed to a shell to interpret. (thus it is exactly the same as the bash script you posted) If you hardcode the entire string that can be okay, but if you get data from somewhere to construct the string you could be executing code that you don't intend! (just FYI)
1

Controlling a python script from another script

You can check the link above, it is indeed similar issue. Using Popen from subprocess or from os.popen it is possible to check real time.

With a simple os.system ("your script > /tmp/mickey.log") will also run the script, but it will wait the execution of the command before.

Please let me know if this solve your issue.

2 Comments

How do I redirect stderr to the log file as well? When I try os.system("my_script &> my_log") it does not create any log file but throwing everything to the python terminal.
In a bash script use 2>&1 will redirect sterr to stdout that your are sending to my log. So for instance: script.py 2>&1 > mickey.log I think this will solve your query :-)

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.