我有一个脚本可以在 USB 驱动器上创建一个名为“视频”的文件夹,将 6,500 个 WMV 文件移动到“视频”文件夹中。然后假设创建一个 HTML 页面,其中包含指向每个文件的超链接。这是我当前损坏的示例。我试图让它抓取视频目录并创建一个仅包含指向 USB 驱动器上本地文件的超链接的 HTML 页面。
#!/usr/bin/python
import os.path
import os
import shutil
import re
# Create the videos directory in the current location
# If the directory exists ignore it
def createDirectory():
directory = "videos"
if not os.path.isdir("./" + directory + "/"):
os.mkdir("./" + directory + "/")
print "Videos Folder Created."
else:
print "Video Folder Exists."
print "---------------------"
# Move all the files in the root directory with the .wmv extension
# to the videos folder
def moveVideos():
for file in os.listdir("."):
if os.path.splitext(file)[1] == ".wmv":
print "Moving:", file
shutil.move(file, os.path.join("videos", file))
def createHTML():
videoDirectory = os.listdir("videos")
f = open("videos.html", "w")
f.writelines(videoDirectory)
r = re.compile(r"(\\[^ ]+)")
print r.sub(r'<a href="\1">\1</a>', videoDirectory)
createDirectory()
moveVideos()
createHTML()
请您参考如下方法:
import cgi
def is_video_file(filename):
return filename.endswith(".wmv") # customize however you like
def createHTML():
videoDirectory = os.listdir("videos")
with open("videos.html", "w") as f:
f.write("<html><body><ul>\n")
for filename in videoDirectory:
if is_video_file(filename):
f.write('<li><a href="%s">%s</a></li>\n' %
(cgi.escape(filename, True), cgi.escape(filename)))
f.write("</ul></body></html>\n")
