【问题标题】:how to set comment in python zipinfo如何在 python zipinfo 中设置注释
【发布时间】:2017-09-27 05:51:40
【问题描述】:

我用库zipfile完成了python challenge中的第6关,发现答案记录在ZipInfo.comment中。我想知道如何在这个字段中输入文本。我已经阅读了python库zipfile的源代码,但找不到任何方法来实现它。

有人知道吗?

【问题讨论】:

    标签: python zipfile


    【解决方案1】:

    你可以在创建ZipFile对象的时候写:

    with zipfile.ZipFile('myzip.zip', 'w') as zip:
        zip.write('file.py')
        zip.comment = b'This is my comment'
    

    文本必须以二进制形式输入,前缀为b

    https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.comment

    如果你的存档已经存在,你也可以使用a模式单独追加评论:

    with zipfile.ZipFile('myzip.zip', 'a') as zip:
        zip.comment = b'This is a new comment'
    

    要对压缩文件设置注释,您必须访问ZipInfo 对象,如下所示,或使用方法from_file 创建它:

    with zipfile.ZipFile('myzip.zip', 'w') as zip:
        zip.write('file.py')
        info = zip.getinfo('file.py')
        info.comment = b'zipped file comment'
    

    【讨论】:

    猜你喜欢
    • 2011-07-04
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多