简体   繁体   English

无法扩展以zip压缩的.zip(不允许操作)

[英]Unable to expand .zip (Operation not permitted) zipped with python

I have a directory with several files. 我有一个包含多个文件的目录。 I want to create an archive with some of these files (eg only files with names listed in some array) That's my code: 我想用其中的一些文件(例如,只列出名称在某些数组中列出的文件)创建档案,这是我的代码:

import zipfile
import os

def zip_files(report_dir):
    zipf = zipfile.ZipFile('report.zip', 'w', zipfile.ZIP_STORED)
    rep = ['1', '2', 'new']
    print('RRREP ', rep)
    for root, dirs, files in os.walk(report_dir):
        for file in files:
            title = file.title().split('_')[0]
            if title in rep:
                zipf.write(os.path.join(root, file))

    zipf.close()


zip_files('')

When I try to expand the resulting archive, I receive a message 当我尝试扩展生成的档案时,我收到一条消息

Unable to expand "report.zip" (Error 1 - Operation not permitted) 无法展开“ report.zip”(错误1-不允许操作)

If I add all files from the directory (= if I remove if... -code), the archive can be opened without problems. 如果我从目录中添加所有文件(=, if...则删除-code),则可以毫无问题地打开存档。

It seems that creating an empty zip file can cause some issues to some external archivers. 似乎创建一个空的zip文件可能会导致某些外部存档程序出现一些问题。

Your filter doesn't match anything. 您的过滤器不匹配任何内容。 In the future, you could protect your archive creation with a flag and report an error if nothing matched: 将来,您可以使用标志来保护创建的归档文件,如果没有匹配项,则报告错误:

at_least_one_match = False

for root, dirs, files in os.walk(report_dir):
    for file in files:
        title = file.title().split('_')[0]
        if title in rep:
            at_least_one_match = True
            zipf.write(os.path.join(root, file))


if not at_least_one_match:
    raise Exception("No files were added. Archive is empty/corrupt")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM