I know that it is easy to run an external command from Python and display the output on the screen. For example: (It will display the output only on the screen)
import subprocess
subprocess.Popen(['dir'])
I also know that we can run an external command from Python and get the output. For example: (It will store the output as a string in variable out and will not display the output on the screen anymore)
import subprocess
result = subprocess.Popen(['dir'], stdout=subprocess.PIPE)
out = result.stdout.read()
My question is: Is there a way to display the output on the screen and store and output in a variable simultaneously? In other words, when I run the code, the external command output will display on the screen. At the same time, a variable in the code can get the output displayed on the screen.
Update: I found out if I include stdout=subprocess.PIPE
in subprocess.Popen
, it will display nothing on the screen (windows command prompt).
Related questions: