r5 = requests.get(requestUrl,headers=requestHeaders,stream=True) time.sleep(3) #Ensure we do not automatically decompress the data on the fly: r5.raw.decode_content = False if useAws: print ('Content response headers (AWS server): type: ' + r5.headers["Content-Type"] + '\n') #AWS does not set header Content-Encoding="gzip". else: print ('Content response headers (TRTH server): type: ' + r5.headers["Content-Type"] + ' - encoding: ' + r5.headers["Content-Encoding"] + '\n') #Next 2 lines display some of the compressed data, but if you uncomment them save to file fails fileName = filePath0 + dateforfilename+".csv.gz" chunk_size = 1024 rr = r5.raw print (rr) with open(fileName, 'wb') as fd: shutil.copyfileobj(rr, fd, chunk_size) fd.close