【问题标题】:python gzip decompress filepython gzip解压文件
【发布时间】:2020-11-20 17:32:27
【问题描述】:

我正在尝试使用文件的位置解压缩文件:

gzip.decompress(input_path)

输入路径为 /media/uploads/dino_test.msh_b1R1dcs.gz

我收到以下错误:

File "/usr/lib64/python3.6/gzip.py", line 531, in decompress
with GzipFile(fileobj=io.BytesIO(data)) as f:
TypeError: a bytes-like object is required, not 'str'

我知道 decompress 需要一个文件而不是路径。如何解压缩知道位置的文件?该文件通过在后端运行的 python 脚本上传并放入该位置。

【问题讨论】:

  • 也许可以试试gzip.open(input_path)

标签: python compression gzip


【解决方案1】:
gz_files = [file for file in os.listdir(input_path) if file.endswith('.gz')]

如果你确定你只有一个文件,那么

gzip.decompress(gz_files[0])

【讨论】:

    【解决方案2】:

    试试下面的代码,解压需要的文件对象。

    with open(input_path) as fp:
       gzip.decompress(fp)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-01
      • 2011-01-26
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 2014-10-21
      • 2012-06-26
      • 1970-01-01
      相关资源
      最近更新 更多