使用 python 2.5 将 zip 文件下载到本地驱动器并将所有文件解压到目标文件夹

发布于 2024-08-12 04:34:18 字数 866 浏览 5 评论 0原文

我正在尝试将 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)

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

I am trying to download a zip file to a local drive and extract all files to a destination folder.

so i have come up with solution but it is only to "download" a file from a directory to another directory but it doesn't work for downloading files. for the extraction, I am able to get it to work in 2.6 but not for 2.5. so any suggestions for the work around or another approach I am definitely open to.
thanks in advance.

######################################
'''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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏日浅笑〃 2024-08-19 04:34:18

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

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)

urllib.urlretrieve can get a file (zip or otherwise;-) from a URL to a given path.

extractall is indeed new in 2.6, but in 2.5 you can use an explicit loop (get all names, open each name, etc). Do you need example code?

So here's the general idea (needs more try/except if you want to give a nice error message in each and every case which could go wrong, of which, of course, there are a million variants -- I'm only using a couple of such cases as examples...):

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)
ゃ懵逼小萝莉 2024-08-19 04:34:18

要下载,请查看 urllib:

import urllib
webFile = urllib.urlopen(url)

要解压缩,请使用 zipfile。另请参阅此示例

For downloading, look at urllib:

import urllib
webFile = urllib.urlopen(url)

For unzipping, use zipfile. See also this example.

记忆で 2024-08-19 04:34:18

到目前为止,我发现的最短方法是使用 +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()

The shortest way i've found so far, is to use +alex answer, but with ZipFile.extractall() instead of the loop:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文