python - 将不同路径的新文件添加到 python 中的现有 zip 文件
问题描述
我有一个路径,路径中的所有文件都应该被压缩,一旦文件被压缩,我需要将不同文件路径中的不同文件添加到现有的 zip 文件夹中。我已经编写了代码来从第一个路径创建和压缩文件。有什么方法可以将不同路径中的文件附加到现有的 zip 文件夹中。
这是我的代码:
def _create_zip_folder(self, zip_path, base_path, pattern='.*'):
zip = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED)
rootlen = len(base_path)
patterns = pattern.split(",")
regex = re.compile('|'.join(fnmatch.translate(p) for p in patterns))
for base, _, files in os.walk(base_path):
for file in files:
if regex.match(file):
fn = os.path.join(base, file)
zip.write(fn, fn[rootlen:])
解决方案
您可以在函数末尾添加:
some_other_path = '/some/other/path/a_file.ext'
name = some_other_path.split( os.path.sep )[-1]
zip.write( some_other_path, name )
推荐阅读
- html - 我可以使用带有复选框的伪元素吗?
- python - 将多维列表分配给唯一值的最佳方法
- react-native - 方法或属性 expo-file-system.downloadAsync 在 android 上不可用,您确定已正确链接所有本机依赖项吗?
- ios - 如何快速访问嵌套 JSON 对象中的值?
- c++ - 基于轮廓的三角剖分
- python - Conda 软件包的完整性是否得到验证?
- excel - 从列表中创建和更新数据矩阵?
- windows - 为什么 XDebug 在不同的网络上无法连接?
- android - Firebase 在 Android 上的 Unity 中使用 Apple 进行身份验证,无需每次都需要登录详细信息
- jinja2 - Jinja 中的简单计数器循环?