我在 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/