如何向Python脚本添加终端命令?
我有一个大文件夹的图片/视频/文件夹更多的图像/视频,我想把它们组织在一个HTML文件(首页.html). 在
脚本首先列出目录中的所有文件:def listFiles():
command = "ls"
output = Popen(command, stdout=PIPE) #This is the only way i found to run LS in terminal and get the output
x = str(output.stdout.read())
x = x[2:-3]
x += (" ")
x = re.sub(r"\\n", " ", x)
y = ""
finalLIST = list()
for o in x:
if o == " ":
finalLIST.append(str(y))
y = ""
else:
y += o
return finalLIST #returns a list with all files in the current directory
然后检查文件是图像还是视频,如果是视频,则添加到HTML文件中:
^{pr2}$
如果它是一个图像,它会添加:
代码是:def organize():
DIRECTORIES = listFiles()
IMAGE = [".png", ".jpg"]
VIDEO = [".webm", ".mp4"]
for x in DIRECTORIES:
if not re.search(".", x):
#This means that it is a directory
#I want to CD into this directory and run listFiles() and then organize() it. How i do it?
else:
for y in IMAGE:
ADDimg = "\n\n"
if re.search(y, x):
with open(FirstPage.html) as f:
for line in f:
if line = "":
f.write(ADDimg)
break
f.write(ADDimg)
for y in VIDEO:
ADDvideo = """\n
"""\" type="video/WebM/mp4">
Video not suported!
\n
"""
if re.search(y, x):
with open(FirstPage.html) as f:
for line in f:
if line = "":
f.write(ADDvideo)
break
这是第一页.html以下内容:
The first page我希望这个脚本列出目录中的文件,将其中的所有图像/视频添加到HTML文件中,然后cd到那里的文件夹中,并递归地执行相同的操作。有什么建议吗?在
谢谢!在