使用 python 到 运行 bash 命令并获得输出
Using python to run bash commands and get Output
我想通过Youtube-he下载视频。所以,我写了程序,但它不是 working.The 代码是
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")
不要使用 os.system("mkdir a")
,而要使用 os.system("mdkir %s" % a)
,否则它会创建一个名为 a 的目录。不评估字符串中的变量名。同理,使用os.system("youtube-dl %s" % b)
。此外,使用 os.system("cd a")
切换到创建的目录,之后不要忘记使用 os.system("cd ..")
切换回来。
您需要传递实际变量,而不是字符串。您还应该特别使用 subprocess 模块 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
将设置下载文件的目录。
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("..")
这似乎可以正常工作。
谢谢你。伙计们帮忙
我想通过Youtube-he下载视频。所以,我写了程序,但它不是 working.The 代码是
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")
不要使用 os.system("mkdir a")
,而要使用 os.system("mdkir %s" % a)
,否则它会创建一个名为 a 的目录。不评估字符串中的变量名。同理,使用os.system("youtube-dl %s" % b)
。此外,使用 os.system("cd a")
切换到创建的目录,之后不要忘记使用 os.system("cd ..")
切换回来。
您需要传递实际变量,而不是字符串。您还应该特别使用 subprocess 模块 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
将设置下载文件的目录。
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("..")
这似乎可以正常工作。 谢谢你。伙计们帮忙