我最近开始接触Python,遇到了一个我认为可以用它解决的问题。 我有一个U盘,里面有很多文件夹,每个文件夹都包含以下内容:
/HTML5/
/Images/
foldername.html
foldername.ofp
preview.html
profile.xml
现在,我必须为每个文件夹创建一个zip文件,但只包含Images文件夹和配置文件.xml文件。那里有40个文件夹,将来还会有更多。你知道吗
以下是最终代码:
# Script to zip Foldername/Images/ and Foldername/profile.xml to Foldername.zip
import os
import zipfile
dirname = "D:\\testdir"
for schoen in os.listdir(dirname):
print schoen
myZipFile = zipfile.ZipFile(schoen+".zip", "w" )
for f in os.listdir(os.path.join(dirname, schoen)):
print f
if f == "Profile.xml":
print "Found profile.xml"
myZipFile.write(schoen + "\\" + f, f)
elif f == "Images":
print "Found Images"
myZipFile.write(schoen + "\\" + f, f)
myZipFile.close()
非常感谢, 格罗特
与其寻找随机代码并猜测它可能意味着什么以及您应该更改什么,为什么不在文档中查找它呢?你知道吗
^{} 的参数是:
因此,您的代码将名为
profile.xml
的文件复制到名为images\\test.py
的存档中。那真的是你想要的吗?我怀疑。但我不知道你想要什么。你需要弄清楚这一点,用你可以向一个白痴解释的话来说(因为计算机是白痴)。如果你被这些文档弄糊涂了,那么这些文档可以帮助你把这些解释翻译成适当的代码。你知道吗相关问题 更多 >
编程相关推荐