【问题标题】:download a zip file to a local drive and extract all files to a destination folder using python 2.5将 zip 文件下载到本地驱动器并使用 python 2.5 将所有文件解压缩到目标文件夹
【发布时间】:2009-11-21 03:44:57
【问题描述】:

我正在尝试将 zip 文件下载到本地驱动器并将所有文件解压缩到目标文件夹。

所以我想出了解决方案,但它只是将一个文件从一个目录“下载”到另一个目录,但它不适用于下载文件。对于提取,我可以让它在 2.6 中工作,但不能在 2.5 中工作。因此,我绝对愿意接受有关解决方法或其他方法的任何建议。 提前致谢。

######################################
'''this part works but it is not good for URl links''' 
import shutil

sourceFile = r"C:\Users\blueman\master\test2.5.zip"
destDir = r"C:\Users\blueman\user"
shutil.copy(sourceFile, destDir)
print "file copied"
######################################################

'''extract works but not good for version 2.5'''
import zipfile

GLBzipFilePath =r'C:\Users\blueman\user\test2.5.zip'
GLBextractDir =r'C:\Users\blueman\user'

def extract(zipFilePath, extractDir):
 zip = zipfile(zipFilePath)
 zip.extractall(path=extractDir)
 print "it works"

extract(GLBzipFilePath,GLBextractDir)

######################################################

【问题讨论】:

    标签: python http download extract unzip


    【解决方案1】:

    urllib.urlretrieve 可以从 URL 到给定路径获取文件(zip 或其他方式;-)。

    extractall 在 2.6 中确实是新的,但在 2.5 中您可以使用显式循环(获取所有名称、打开每个名称等)。需要示例代码吗?

    所以这是一般的想法(需要更多try/except,如果你想在每个可能出错的情况下给出一个很好的错误消息,当然,其中有一百万种变体 -我只是用几个这样的例子作为例子......):

    import os
    import urllib
    import zipfile
    
    def getunzipped(theurl, thedir):
      name = os.path.join(thedir, 'temp.zip')
      try:
        name, hdrs = urllib.urlretrieve(theurl, name)
      except IOError, e:
        print "Can't retrieve %r to %r: %s" % (theurl, thedir, e)
        return
      try:
        z = zipfile.ZipFile(name)
      except zipfile.error, e:
        print "Bad zipfile (from %r): %s" % (theurl, e)
        return
      for n in z.namelist():
        dest = os.path.join(thedir, n)
        destdir = os.path.dirname(dest)
        if not os.path.isdir(destdir):
          os.makedirs(destdir)
        data = z.read(n)
        f = open(dest, 'w')
        f.write(data)
        f.close()
      z.close()
      os.unlink(name)
    

    【讨论】:

    • 是的,我是 python 的超级新手。感谢您的指示
    • 我在修改脚本有一段时间了,我必须回来。尽管“for n in z.namelist():”指的是所有文件。我似乎无法解压缩 zipfile 中的文件夹并维护 zip 文件中的文件结构。再次感谢
    • @marcus,我提供的代码对我来说非常有用:为什么不准确发布您遇到的错误,而不是完全通用的“似乎无法”?!显然没有人可以在没有信息的情况下帮助您。
    【解决方案2】:

    要下载,看urllib:

    import urllib
    webFile = urllib.urlopen(url)
    

    要解压缩,请使用zipfile。另见this example

    【讨论】:

    • 我链接的示例可能适用于 Python 2.5,因为它不使用新函数 ZipFile.extractall。
    【解决方案3】:

    到目前为止,我发现的最短方法是使用 +alex 答案,但使用 ZipFile.extractall() 而不是循环:

    from zipfile import ZipFile
    from urllib import urlretrieve
    from tempfile import mktemp
    
    filename = mktemp('.zip')
    destDir = mktemp()
    theurl = 'http://www.example.com/file.zip'
    name, hdrs = urlretrieve(theurl, filename)
    thefile=ZipFile(filename)
    thefile.extractall(destDir)
    thefile.close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-29
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 2016-04-20
      相关资源
      最近更新 更多