首页 > 解决方案 > 使用 Python 解压 gzip 文件时失败

问题描述

我从 Azure下载了一个“myfile.tsv.gz”文件(使用azcopy)。我想用 Python 打开它。

import gzip
f = gzip.open('myfile.tsv.gz', 'rb')
file_content = f.read()

但是对于 macOS 上的 Python 2.7.16 和 Python 3.9.5,我收到了错误消息:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/gzip.py", line 300, in read
    return self._buffer.read(size)
  File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/gzip.py", line 495, in read
    uncompress = self._decompressor.decompress(buf, size)
zlib.error: Error -3 while decompressing data: invalid block type

我真的不知道我该如何解决它。

仅供参考,在我的终端上,此命令产生:

>gunzip -t myfile.tsv.gz

gunzip: data stream error 
gunzip: myfile.tsv.gz: uncompress failed

尽管如此,此命令似乎可以正确打印文件内容:

>gunzip -c myfile.tsv.gz

[correct file content]

我也试过重新下载这个文件,同样的问题......

谢谢!

标签: pythonfilegzipgunzip

解决方案


推荐阅读