gpt4 book ai didi

python - 如何在 python zipinfo 中设置注释

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:30 25 4
gpt4 key购买 nike

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

有人知道吗?

最佳答案

您可以在创建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'

关于python - 如何在 python zipinfo 中设置注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46440381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com