Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python 2.5将zip文件下载到本地驱动器,并将所有文件解压缩到目标文件夹_Python_Http_Download_Extract_Unzip - Fatal编程技术网

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

使用python 2.5将zip文件下载到本地驱动器,并将所有文件解压缩到目标文件夹,python,http,download,extract,unzip,Python,Http,Download,Extract,Unzip,我正在尝试将zip文件下载到本地驱动器,并将所有文件解压缩到目标文件夹 所以我提出了一个解决方案,但它只是将一个文件从一个目录“下载”到另一个目录,但它不适用于下载文件。对于提取,我可以让它在2.6中工作,但不能在2.5中工作。因此,任何关于工作方法或其他方法的建议我都绝对愿意接受。 提前谢谢 ###################################### '''this part works but it is not good for URl links''' import

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

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

要下载,请查看urllib:

import urllib
webFile = urllib.urlopen(url)
要解压缩,请使用。另请参见。

可以从URL获取到给定路径的文件(zip或其他;-)

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

因此,这里是总体思路(需要更多的
尝试
/
,除了
,如果您想在每种可能出错的情况下都给出一个好的错误消息,当然,其中有一百万种变体——我只使用了两种这样的情况作为示例…):


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

是的,我是python的超级新手。谢谢你的指导,我在修改脚本有一段时间了,我必须回来。尽管“for n in z.namelist():”指的是所有文件。我似乎无法在zipfile中解压文件夹并在zip文件中维护文件结构。谢谢你more@marcus,我给出的代码对我来说非常有用:为什么不发布您得到的确切错误,而不是完全通用的“似乎无法”?!显然,没有信息谁也帮不了你。我链接的示例可能在Python 2.5中工作,因为它没有使用新函数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()