Download and decompress gzipped file in memory?

后端 未结 4 504
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 11:05

I would like to download a file using urllib and decompress the file in memory before saving.

This is what I have right now:



        
4条回答
  •  情深已故
    2020-12-02 11:42

    For those using Python 3, the equivalent answer is:

    import urllib.request
    import io
    import gzip
    
    response = urllib.request.urlopen(FILE_URL)
    compressed_file = io.BytesIO(response.read())
    decompressed_file = gzip.GzipFile(fileobj=compressed_file)
    
    with open(OUTFILE_PATH, 'wb') as outfile:
        outfile.write(decompressed_file.read())
    

提交回复
热议问题