如何在IPYTHON中提取压缩的zip文件?

时间:2014-12-08 05:29:28

标签: python python-2.7 zipfile

我开始通过运行代码手册从书中学习IPYTHON。正如书中给出的那样,我无法使用相同的输出运行代码或没有错误,我有点被困在这一行并出现错误。

我可以找到如何在与其他来源不同的上下文中在线提取zip文件,但我想解决此问题,以便我知道如何处理错误或在以后继续进行故障排除。我想知道这里发生错误的错误,同时没有向本书的作者显示错误。我在这里错过了任何细节吗?我是否错过了根据上下文等更改文件名。以下是我正在运行的环境中的代码和错误。

Python 2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Jul  2 2014, 15:12:11) [MSC v.1
500 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 2.2.0 -- An enhanced Interactive Python.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import urllib2, zipfile

In [2]: url = 'http://ipython.rossant.net/'

In [3]: filename = 'facebook.zip'

In [4]: downloaded = urllib2.urlopen(url + filename)

In [5]: folder = 'data'

In [6]: mkdir $folder
A subdirectory or file data already exists.

In [7]: mkdir $folder1

In [8]: cd $folder1
C:\Users\think\Documents\Python Scripts\$folder1

In [9]: with open(filename, 'wb') as f:
   ...:     f.write(downloaded.read())
   ...:

In [10]: with zipfile.ZipFile(filename) as zip:
   ....:     zip.extractall('.')
   ....:
 ---------------------------------------------------------------------------
BadZipfile                                Traceback (most recent call last)
<ipython-input-10-e24f3f3a7f9c> in <module>()
----> 1 with zipfile.ZipFile(filename) as zip:
      2     zip.extractall('.')
      3

C:\Users\think\Anaconda\lib\zipfile.pyc in __init__(self, file, mode, compressio
n, allowZip64)
    768         try:
    769             if key == 'r':
--> 770                 self._RealGetContents()
    771             elif key == 'w':
    772                 # set the modified flag so central directory gets writte
n

C:\Users\think\Anaconda\lib\zipfile.pyc in _RealGetContents(self)
    809             raise BadZipfile("File is not a zip file")
    810         if not endrec:
--> 811             raise BadZipfile, "File is not a zip file"
    812         if self.debug > 1:
    813             print endrec

BadZipfile: File is not a zip file

In [11]:

感谢任何帮助或指导。

2 个答案:

答案 0 :(得分:2)

您只需将文件命名为facebook.zip,但不会将其设置为zip文件(这就是您收到该错误的原因)。就像我创建文本文件hello.txt然后将其重命名为hello.png一样,它将不会转换为图像。

尝试将其作为普通文件阅读:

with open(filename) as foo:
   for line in foo:
      print(line)

答案 1 :(得分:2)

这是一个已知问题:请参阅this link。问题基本上来自包含该文件的服务器中的错误配置:从Python下载此文件有时会导致文件损坏。最简单的修复方法是通过Web浏览器手动下载文件并将其解压缩到工作目录中。