0U0*_*0e0 6 html python linux python-3.5
如何将终端命令添加到我的 Python 脚本中?我有一个包含更多图像/视频的图像/视频/文件夹的巨大文件夹,我想将它们组织在一个 HTML 文件 (FirstPage.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
Run Code Online (Sandbox Code Playgroud)
然后检查文件是图像还是视频,如果是视频,则添加到 HTML 文件中:
<video controls>
<source src="videoName.mp4" type="video/WebM/mp4">
<source src="videoName.ogg" type="video/ogg">
Video not suported!
</video>
Run Code Online (Sandbox Code Playgroud)
如果它是一个图像,它会添加:
<img src="ImageName.jpg" alt="image"/>
Run Code Online (Sandbox Code Playgroud)
代码是:
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<img src=\"" + x + "\" alt=\"imagem\"/>\n"
if re.search(y, x):
with open(FirstPage.html) as f:
for line in f:
if line = "<!--IMAGES-->":
f.write(ADDimg)
break
f.write(ADDimg)
for y in VIDEO:
ADDvideo = """\n<video controls>
<source src=\"""" + x
"""\" type="video/WebM/mp4">
<source src="video.ogg" type="video/ogg/WebM">
Video not suported!
</video>\n
"""
if re.search(y, x):
with open(FirstPage.html) as f:
for line in f:
if line = "<!--VIDEOS-->":
f.write(ADDvideo)
break
Run Code Online (Sandbox Code Playgroud)
这是 FirstPag.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>The first page</title>
</head>
<body>
<!--IMAGES-->
<!--VIDEOS-->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我希望这个脚本列出目录中的文件,将那里的所有图像/视频添加到 HTML 文件中,然后 cd 到那里的文件夹中,并递归地执行相同的操作。有什么建议吗?
谢谢!
不要对语言库中存在的内容执行cd/命令: 。lsos.listdir()
你可以这样使用它:
from os import listdir
from os.path import isfile, join
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
Run Code Online (Sandbox Code Playgroud)
同样,您可以使用它isdir来检查目录。
可以组合上面的命令来进行递归目录遍历。
| 归档时间: |
|
| 查看次数: |
1813 次 |
| 最近记录: |