Downloading and unzipping a .zip file without writing to disk

前端 未结 9 1585
囚心锁ツ
囚心锁ツ 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:23

    I'd like to add my Python3 answer for completeness:

    from io import BytesIO
    from zipfile import ZipFile
    import requests
    
    def get_zip(file_url):
        url = requests.get(file_url)
        zipfile = ZipFile(BytesIO(url.content))
        zip_names = zipfile.namelist()
        if len(zip_names) == 1:
            file_name = zip_names.pop()
            extracted_file = zipfile.open(file_name)
            return extracted_file
        return [zipfile.open(file_name) for file_name in zip_names]
    

提交回复
热议问题