-4

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.")
    

2 Answers2

1

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'])

Python docs to subprocess

sudo
  • 906
  • 2
  • 14
  • 32
  • `ls` is (at least on Unix/Linux) systems also available as a binary, you are not invoking the `ls` from your shell (an certainly not `cmd`) – Anthon Apr 23 '17 at 06:03
  • @Anthon yes i just gave a simple example of call and nothing was coming to my mind – sudo Apr 23 '17 at 06:09
0

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.

Anthon
  • 69,918
  • 32
  • 186
  • 246