如何删除或替换 zip 存档中的文件? [英] How do I delete or replace a file in a zip archive?

查看:52
本文介绍了如何删除或替换 zip 存档中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Python 创建一个程序,该程序会下载一组文件并将它们与 zipfile 模块一起放入存档中.

I'm creating a program in Python which downloads a set of files and puts them into an archive with the zipfile module.

我已经知道如何附加到存档中,但有些情况下存档中的文件已经存在并且应该被覆盖.

I already found out how to append to the archive, but there are cases where the files in the archive already exist and should be overwritten.

目前,如果我将一个已经存在的文件附加到存档中,我会得到一个副本.

Currently, if I append an already existing file to the archive I get a duplicate.

有人知道如何删除存档中的文件吗?

Does anyone know how to delete a file in an archive?

推荐答案

来自 http://docs.python.org/2/library/zipfile

ZipFile.namelist()
Return a list of archive members by name.

因此,在附加到文件并针对存档中的现有成员列表执行检查操作之前获取成员列表是微不足道的.

So it is trivial to get hold of the members list before appending to the file and performing a check operation against the list of existing members within the archive.

另外:不支持从 ZIP 文件中删除.你需要写一个新的存档如果需要,复制现有文件并省略要删除的文件.

In addition: removing from a ZIP file is not supported. You need to write a new archive if needed and copy over existing files and omit the file to be removed.

另见

使用 ZipFile 模块从 zipfile 中删除文件

这篇关于如何删除或替换 zip 存档中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆