首页 / 操作系统 / Linux / Python zipfile报错解决一例
最近用Python 来读zip的压缩包 。报一个错误。Python 2.6.6 (r266:84292, Jun 18 2012, 14:18:47)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import zipfile
>>> zipfile.ZipFile("bla.apk")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/zipfile.py", line 696, in __init__
self._GetContents()
File "/usr/lib64/python2.6/zipfile.py", line 716, in _GetContents
self._RealGetContents()
File "/usr/lib64/python2.6/zipfile.py", line 772, in _RealGetContents
x._decodeExtra()
File "/usr/lib64/python2.6/zipfile.py", line 351, in _decodeExtra
tp, ln = unpack("<HH", extra[:4])
struct.error: unpack requires a string argument of length 4这应该算是 Python的一个bug ,详细的在 http://bugs.python.org/issue14315 这里有讨论。解决方法有两个。一是 : 按照这个网页所讨论的, 打一个补丁 。http://bugs.python.org/file24902/fix_zipfile_extra.patch 。具体就是 进入 /usr/lib64/python2.6/zipfile.py 这个文件 ,在 351行附近 ,把 while extra: 改成 while len(extra) >= 4:方法二是 升级 Python版本 。在Python 2.6.6 上发现了这个问题 。这个Python是Linux自带的版本,可能比较低。升级2.7.3 以上的版本 ,在高版本中这个问题已经被解决了。 所以升级Python也是个办法。--------------------------------------分割线 --------------------------------------CentOS上源码安装Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htmPython脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htmPython 语言的发展简史 http://www.linuxidc.com/Linux/2014-09/107206.htmPython 的详细介绍:请点这里
Python 的下载地址:请点这里本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-02/112781.htm