0

我正在尝试使用子进程库。

$youtube-dl http://www.youtube.com/watch?v=co5gy_2uOEY在终端上按预期工作,但在 IDLE 中运行的以下代码片段似乎没有做任何事情。

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

或者,我也尝试过:

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

但这又回来1了,也没有做任何事情。这里发生了什么?

编辑:

用双引号换行使其工作,但现在这个子进程挂起。我尝试进行以下更改,但它再次不起作用:

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()
4

3 回答 3

1

不知道为什么你没有得到关于http未定义的错误,但你应该使用:

line = "http://www.youtube.com/watch?v=co5gy_2uOEY"

将字符串传递给subprocess.call

于 2013-02-22T00:01:51.237 回答
1

它挂起,因为它有效。您可以检查运行输出idle的终端。youtube-dl

于 2013-02-22T00:41:27.983 回答
-2

您是否尝试添加 shell=True?(尽管通常不鼓励这样做。)

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

于 2013-02-22T00:05:49.677 回答