提取某些zip文件时出现Python错误

提取某些zip文件时出现Python错误,python,zip,urllib2,zipfile,Python,Zip,Urllib2,Zipfile,我编写了一个小应用程序,在链接提供时下载一个zip文件(具有不同的扩展名),并将文件解压缩到重命名的文件夹中。 出于某种原因,它适用于我的一些zip文件,但不是所有的 我得到一个: Traceback (most recent call last): File "download_unzip.py", line 48, in <module> main() File "download_unzip.py", line 42, in main shutil.mo

我编写了一个小应用程序,在链接提供时下载一个zip文件(具有不同的扩展名),并将文件解压缩到重命名的文件夹中。 出于某种原因,它适用于我的一些zip文件,但不是所有的

我得到一个:

Traceback (most recent call last):
  File "download_unzip.py", line 48, in <module>
    main()
  File "download_unzip.py", line 42, in main
    shutil.move(unzip_file(temp_kmz),'temp_extracted/')
  File "download_unzip.py", line 26, in unzip_file
    fd = open(name, 'w')
IOError: [Errno 2] No such file or directory: 'models/model.dae'
我的工作下载文件:

python下载\u unzip.py“”

不起作用的:

python下载_unzip.py “”


请注意,这两个文件都是使用我的操作系统(Ubuntu)正确提取的。

修复了我的一些重大代码更改问题:

import urllib2 ,argparse, shutil, urlparse , os , zipfile, os.path
from zipfile import ZipFile as zip

parser = argparse.ArgumentParser(description="Download and Unzip")
parser.add_argument('url', help='The action to take (e.g. install, remove, etc.)')
args = parser.parse_args()

print args.url

url = args.url
temp_kmz="temp_kmz"

def extractAll(zipName):
    z = zip(zipName)
    for f in z.namelist():
        if f.endswith('/'):
            os.makedirs(f)
        else:
            z.extract(f)

def download(url, fileName=None):
    def getFileName(url,openUrl):
        if 'Content-Disposition' in openUrl.info():
            # If the response has Content-Disposition, try to get filename from it
            cd = dict(map(
                lambda x: x.strip().split('=') if '=' in x else (x.strip(),''),
                openUrl.info()['Content-Disposition'].split(';')))
            if 'filename' in cd:
                filename = cd['filename'].strip("\"'")
                if filename: return filename
        # if no filename was found above, parse it out of the final URL.
        return os.path.basename(urlparse.urlsplit(openUrl.url)[2])

    r = urllib2.urlopen(urllib2.Request(url))
    try:
        fileName = fileName or getFileName(url,r)
        with open(fileName, 'wb') as f:
            shutil.copyfileobj(r,f)
    finally:
        r.close()

def main():
    download(url,temp_kmz)
    extractAll(temp_kmz)
main()

我发现你的代码有很多问题。对于初学者来说,
unzip_file()
函数被传递zip存档文件名,并尝试从中提取所有文件,但只返回第一个文件的名称,最后一个字符被删除(因此无效,这将导致以下
shutil.move()
调用失败)。另外,请不要只是说“不起作用”。至少要详细描述问题中发生的任何回溯,包括回溯的方式。基本思想是帮助我们帮助你。我去掉最后一个字符,因为它是“/”,我认为最好删除它。不管怎么说,即使我离开它也不行。关于回溯,如果您看到代码的第一部分,您将在那里看到它;)如果您需要有关路径设置的帮助,我只需解压缩文件并使用路径读取文件夹,请单击此处>>>
import urllib2 ,argparse, shutil, urlparse , os , zipfile, os.path
from zipfile import ZipFile as zip

parser = argparse.ArgumentParser(description="Download and Unzip")
parser.add_argument('url', help='The action to take (e.g. install, remove, etc.)')
args = parser.parse_args()

print args.url

url = args.url
temp_kmz="temp_kmz"

def extractAll(zipName):
    z = zip(zipName)
    for f in z.namelist():
        if f.endswith('/'):
            os.makedirs(f)
        else:
            z.extract(f)

def download(url, fileName=None):
    def getFileName(url,openUrl):
        if 'Content-Disposition' in openUrl.info():
            # If the response has Content-Disposition, try to get filename from it
            cd = dict(map(
                lambda x: x.strip().split('=') if '=' in x else (x.strip(),''),
                openUrl.info()['Content-Disposition'].split(';')))
            if 'filename' in cd:
                filename = cd['filename'].strip("\"'")
                if filename: return filename
        # if no filename was found above, parse it out of the final URL.
        return os.path.basename(urlparse.urlsplit(openUrl.url)[2])

    r = urllib2.urlopen(urllib2.Request(url))
    try:
        fileName = fileName or getFileName(url,r)
        with open(fileName, 'wb') as f:
            shutil.copyfileobj(r,f)
    finally:
        r.close()

def main():
    download(url,temp_kmz)
    extractAll(temp_kmz)
main()