I have a simple function for executing terminal commands and it looks something like this:
import subprocess
def func(command):
subprocess.run(command, shell=True)
func('python my_python_program.py --par my_parameter_file.py')
The function indeed executes the command, but there should be some output/print statements coming from the program I am executing. The program takes about 10 min to execute and creates a lot of print statements. They get displayed when I just execute the command in a terminal, but subprocess
doesn't display them.
How can I get the output to be printed out when using subprocess
? Can you provide me with 2 different options - 1) if I want the output just printed on the screen, each line immediately, not everything when the command is completely done ; 2) if I want the output to be saved into a file, also immediately line after line? Tnx!