os.walk 将某些文件类型放入现有的 ZipFile 中?
基本上,我需要将文件夹内某种类型的所有文件复制到最高级别的 ZipFile 中(因此不在文件夹内)。
例如,我有一个文件夹“A 文件夹”,该文件夹中包含大量 .png 文件。我想将所有这些文件复制到现有的 ZipFile 中。目前我也只能复制文件夹,所以我最终得到的是 'ZipFile\A Folder\lots of .pngs'
而不是 'ZipFile\lots of .pngs'
我用来移动文件和文件夹的代码是:
for root, dirs, files in os.walk('A Folder'):
for f in files:
fname = os.path.join(root, f)
myzip.write(fname)
另一个快速的事情,您将如何从 ZipFile
中删除文件夹?
Basically I need to copy all files of a certain type within a folder into a ZipFile
at the highest level (so not within a folder).
For example I have a folder, 'A Folder' and within that folder are a load of .png files. I want to copy all those files into an existing ZipFile
. Currently I can only get the Folder to copy as well, so I end up with 'ZipFile\A Folder\lots of .pngs'
rather than 'ZipFile\lots of .pngs'
The code I'm using to move the files and folder is:
for root, dirs, files in os.walk('A Folder'):
for f in files:
fname = os.path.join(root, f)
myzip.write(fname)
Another quick thing, how would you go about deleting a folder from within a ZipFile
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据python文档, zipfile.write() 方法支持第二个参数,即目标名称(即存档中的名称),尝试像这样使用它:
According to the python docs, the zipfile.write() method supports a second argument that is the destination name (ie the name in archive), try tto use it like this :