Downloading and unzipping a .zip file without writing to disk

前端 未结 9 1563
囚心锁ツ
囚心锁ツ 2020-12-02 04:42

I have managed to get my first python script to work which downloads a list of .ZIP files from a URL and then proceeds to extract the ZIP files and writes them to disk.

9条回答
  •  旧巷少年郎
    2020-12-02 05:26

    It wasn't obvious in Vishal's answer what the file name was supposed to be in cases where there is no file on disk. I've modified his answer to work without modification for most needs.

    from StringIO import StringIO
    from zipfile import ZipFile
    from urllib import urlopen
    
    def unzip_string(zipped_string):
        unzipped_string = ''
        zipfile = ZipFile(StringIO(zipped_string))
        for name in zipfile.namelist():
            unzipped_string += zipfile.open(name).read()
        return unzipped_string
    

提交回复
热议问题