首页 > 解决方案 > 如何在python中读取所有zipfile

问题描述

例如,我有一个 zipfile,我更改了 zip 中的一些文件,我需要对其进行加密,然后保存

def encrypt(self, zipfile: ZipFile):
    import base64
    with open(self.__db_path, 'wb') as db_file:
        decrypted_data = zipfile.read()
    
    aes = AES.new(self.key, AES.MODE_OFB)

    encrypted_data = aes.encrypt(decrypted_data)

    with open(self.__db_path, 'wb') as db_file:
        db_file.write(encrypted_data)

但是这段代码抛出异常,导致代码预期参数“名称”将一些文件读入 zip 我如何读取所有 zip 文件以加密并保存?

标签: pythonencryptionzipzipfilepycryptodome

解决方案


.write使用方法时,您实际上应该指定 zipfile 的名称。

最后一行是

db_file.write(encrypted_data, zipfile)


推荐阅读