使用python运行bash命令并获取输出

时间:2015-04-13 16:33:38

标签: python linux bash youtube downloading

我想通过Youtube-he下载视频。所以,我写了程序,但它没有工作。代码是

import os
l=["sets","relation_and_functions","Trig","Complex_Quad","Linear_inequalities","Permutation","Binomial","Sequence","Straight","conic","Three_d","Limit_Derivative","Stats","Probability","Math_reasoning"]
r=["https://www.youtube.com/playlist?list=PLD5EF274490578CC4","https://www.youtube.com/playlist?list=PL548FAD237A4B6D2E","https://www.youtube.com/playlist?list=PL42123C3873AED16F","https://www.youtube.com/playlist?list=PL880E3116D67E42FD","https://www.youtube.com/playlist?list=PLC5D1199BAE318878","https://www.youtube.com/playlist?list=PL812413BD6B55AA6E","https://www.youtube.com/playlist?list=PL78FAFAFA8496BCE1","https://www.youtube.com/playlist?list=PL6F57725E2DA8B557","https://www.youtube.com/playlist?list=PLA243228EA688A835","https://www.youtube.com/playlist?list=PL41B47FB4E23B392A","https://www.youtube.com/playlist?list=PLa2X112u1cdjdl-dsLJC1HaCeGrOarZZz","https://www.youtube.com/playlist?list=PLF6640F0A9F39F7BA","https://www.youtube.com/playlist?list=PL6FDE1AB3AE32E614","https://www.youtube.com/playlist?list=PL7FF5AD1CFF0981E5","https://www.youtube.com/playlist?list=PL13B63CD6FFAB9EA8"]
for i in range(len(l)):
    a=l[i]
    b=r[i]
    os.system("mkdir a")
    os.chdir("a")
    os.system("youtube-dl b")

3 个答案:

答案 0 :(得分:2)

您需要传递实际变量,而不是字符串。您还应该使用子进程模块,特别是check_call

l =["sets","relation_and_functions","Trig","Complex_Quad","Linear_inequalities","Permutation","Binomial","Sequence","Straight","conic","Three_d","Limit_Derivative","Stats","Probability","Math_reasoning"]
r=["https://www.youtube.com/playlist?list=PLD5EF274490578CC4","https://www.youtube.com/playlist?list=PL548FAD237A4B6D2E","https://www.youtube.com/playlist?list=PL42123C3873AED16F","https://www.youtube.com/playlist?list=PL880E3116D67E42FD","https://www.youtube.com/playlist?list=PLC5D1199BAE318878","https://www.youtube.com/playlist?list=PL812413BD6B55AA6E","https://www.youtube.com/playlist?list=PL78FAFAFA8496BCE1","https://www.youtube.com/playlist?list=PL6F57725E2DA8B557","https://www.youtube.com/playlist?list=PLA243228EA688A835","https://www.youtube.com/playlist?list=PL41B47FB4E23B392A","https://www.youtube.com/playlist?list=PLa2X112u1cdjdl-dsLJC1HaCeGrOarZZz","https://www.youtube.com/playlist?list=PLF6640F0A9F39F7BA","https://www.youtube.com/playlist?list=PL6FDE1AB3AE32E614","https://www.youtube.com/playlist?list=PL7FF5AD1CFF0981E5","https://www.youtube.com/playlist?list=PL13B63CD6FFAB9EA8"]

from subprocess import check_call

for direc, url in zip(l, r):
     check_call(["mkdir", direc])
     check_call(["youtube-dl", ele2],cwd=direc)

zip(l, r)从每个列表中拉出相应的元素,因此我们只需在循环中解包对。 cwd=direc将设置目录以将文件下载到。

答案 1 :(得分:-1)

不要使用os.system("mkdir a"),而是使用os.system("mdkir %s" % a),否则会创建一个名为a的目录。不评估字符串中的变量名称。出于同样的原因,请使用os.system("youtube-dl %s" % b)。此外,使用os.system("cd a")更改为创建的目录,并且不要忘记在此之后使用os.system("cd ..")进行更改。

答案 2 :(得分:-1)

import os
l=["sets","relation_and_functions","Trig","Complex_Quad","Linear_inequalities","Permutation","Binomial","Sequence","Straight","conic","Three_d","Limit_Derivative","Stats","Probability","Math_reasoning"]
r=["https://www.youtube.com/playlist?list=PLD5EF274490578CC4","https://www.youtube.com/playlist?list=PL548FAD237A4B6D2E","https://www.youtube.com/playlist?list=PL42123C3873AED16F","https://www.youtube.com/playlist?list=PL880E3116D67E42FD","https://www.youtube.com/playlist?list=PLC5D1199BAE318878","https://www.youtube.com/playlist?list=PL812413BD6B55AA6E","https://www.youtube.com/playlist?list=PL78FAFAFA8496BCE1","https://www.youtube.com/playlist?list=PL6F57725E2DA8B557","https://www.youtube.com/playlist?list=PLA243228EA688A835","https://www.youtube.com/playlist?list=PL41B47FB4E23B392A","https://www.youtube.com/playlist?list=PLa2X112u1cdjdl-dsLJC1HaCeGrOarZZz","https://www.youtube.com/playlist?list=PLF6640F0A9F39F7BA","https://www.youtube.com/playlist?list=PL6FDE1AB3AE32E614","https://www.youtube.com/playlist?list=PL7FF5AD1CFF0981E5","https://www.youtube.com/playlist?list=PL13B63CD6FFAB9EA8"]
c=[]
for i in l:
    c.append(i)
print c
for i in range(len(l)):
    q="mkdir "+l[i]
    l[i]=q
for j in range(len(r)):
    z="youtube-dl "+r[j]
    r[j]=z
print c
for i in range(len(l)):
    a=l[i]
    e=c[i]
    b=r[i]
    os.system(a)
    os.chdir(e)
    os.system(b)
    os.chdir("..")

这似乎运作正常。 谢谢。伙计们的帮助