download a zip file to a local drive and extract all files to a destination folder using python 2.5

前端 未结 3 720
孤城傲影
孤城傲影 2021-01-01 07:40

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 f

3条回答
  •  粉色の甜心
    2021-01-01 08:21

    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()
    

提交回复
热议问题