簡體   English   中英

python gzip解壓文件

[英]python gzip decompress file

我正在嘗試使用它的位置解壓縮文件:

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'

我知道解壓需要一個文件而不是路徑。 如何解壓縮知道位置的文件? 該文件由在后端運行的 python 腳本上傳並放入該位置。

gz_files = [file for file in os.listdir(input_path) if file.endswith('.gz')]

如果您確定只有一個文件,那么

gzip.decompress(gz_files[0])

試試下面的代碼,解壓需要一個文件對象。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM