How do I set permissions (attributes) on a file in a ZIP file using Python#39;s zipfile module?(如何使用 Python 的 zipfile 模块对 ZIP 文件中的文件设置权限(属性)?)
问题描述
当我从使用 Python 创建的 ZIP 文件中提取文件时 zipfile
模块,所有文件不可写,只读等.
When I extract files from a ZIP file created with the Python zipfile
module, all the files are not writable, read only etc.
文件正在 Linux 和 Python 2.5.2 下创建和提取.
The file is being created and extracted under Linux and Python 2.5.2.
据我所知,我需要为每个文件设置 ZipInfo.external_attr
属性,但这似乎没有记录在我能找到的任何地方,谁能启发我?
As best I can tell, I need to set the ZipInfo.external_attr
property for each file, but this doesn't seem to be documented anywhere I could find, can anyone enlighten me?
推荐答案
这似乎可行(感谢 Evan,将其放在这里,以便该行符合上下文):
This seems to work (thanks Evan, putting it here so the line is in context):
buffer = "path/filename.zip" # zip filename to write (or file-like object)
name = "folder/data.txt" # name of file inside zip
bytes = "blah blah blah" # contents of file inside zip
zip = zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED)
info = zipfile.ZipInfo(name)
info.external_attr = 0777 << 16L # give full access to included file
zip.writestr(info, bytes)
zip.close()
我仍然希望看到记录此内容的内容...我发现的另一个资源是有关 Zip 文件格式的注释:http://www.pkware.com/documents/casestudies/APPNOTE.TXT
I'd still like to see something that documents this... An additional resource I found was a note on the Zip file format: http://www.pkware.com/documents/casestudies/APPNOTE.TXT
这篇关于如何使用 Python 的 zipfile 模块对 ZIP 文件中的文件设置权限(属性)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Python 的 zipfile 模块对 ZIP 文件中的文件设置权限(属性)?


基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01