Issue
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!
Solution
Based on the second link by C.Nivs I found an answer in another post that solves my issue perfectly by using a new, free package called comman_runner. Here is how my code would look like and would produce my desired screen output:
# pip install command_runner # install the package
from command_runner import command_runner # import the package
def func(command):
exit_code, output = command_runner(command, shell=True, live_output=True)
func('python my_python_program.py --par my_parameter_file.py')
Answered By - NeStack Answer Checked By - Candace Johnson (WPSolving Volunteer)