Python – Running shell command and capturing the output in realtime
Question:
This code Running shell command and printing the output in real time.
process = subprocess.Popen('yt-dlp https://www.youtube.com/watch?v=spvPvXXu36A', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
output = process.stdout.readline().decode()
if output == '' and process.poll() is not None:
break
if output:
print(output.strip())
rc = process.poll()
if rc == 0:
print("Command succeeded.")
else:
print("Command failed.")
Answers:
You can use the subprocess module to do all that kind of stuff
I’ve included a small example below
from subprocess import call
call(['youtube-dl', 'https://www.youtube.com/watch?v=PT2_F-1esPk'])
You are calling the exectuable --youtube-dl
which probably not exists.
If --youtube-dl
is a command you can type in from the cmd
prompt, you should try subprocess.check_output(['--youtube-dl', some_url], shell=True)
then the cmd.exe
(at least on Windows) will get invoked.
This code Running shell command and printing the output in real time.
process = subprocess.Popen('yt-dlp https://www.youtube.com/watch?v=spvPvXXu36A', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
output = process.stdout.readline().decode()
if output == '' and process.poll() is not None:
break
if output:
print(output.strip())
rc = process.poll()
if rc == 0:
print("Command succeeded.")
else:
print("Command failed.")
You can use the subprocess module to do all that kind of stuff
I’ve included a small example below
from subprocess import call
call(['youtube-dl', 'https://www.youtube.com/watch?v=PT2_F-1esPk'])
You are calling the exectuable --youtube-dl
which probably not exists.
If --youtube-dl
is a command you can type in from the cmd
prompt, you should try subprocess.check_output(['--youtube-dl', some_url], shell=True)
then the cmd.exe
(at least on Windows) will get invoked.