Search code examples
pythonsubprocessyoutube-dl

Why is my subprocess call failing?


I am trying to play with the subprocess library.

$youtube-dl http://www.youtube.com/watch?v=co5gy_2uOEY on the terminal works as expected but the following code snippet run in the IDLE doesn't seem to do anything.

> os.chdir('/home/andrew')
> line = 'http://www.youtube.com/watch?v=co5gy_2uOEY'
> yt_dl = subprocess.call(['youtube-dl',line])
1

Alternatively, I also tried:

> yt_dl = subprocess.Popen(['youtube-dl',line])

but that returned 1 and didn't do anything either. What's going on here?

EDIT:

Wrapping line with double quotes made it work, but now this subprocess hangs. I tried to make the following change, and it didn't work yet again:

yt_dl = ["youtube-dl","http://www.youtube.com/watch?v=co5gy_2uOEY"]
x = subprocess.Popen(yt_dl, stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True)
stdout, stderr = x.communicate()

Solution

  • It hangs because it works. You can check the terminal from which you run idle for the youtube-dl output.