【发布时间】:2018-07-06 02:12:35
【问题描述】:
我想在 Python 脚本中模拟 gzip -d <file.gz> 的行为。
压缩后的 GZIP 文件被解压并写入为与原始 GZIP 文件同名的文件,不带 .gz 扩展名。
file.abc.gz --> 文件.abc
使用 gzip 库如何做到这一点并不明显,文档中的所有示例都是用于压缩数据数组,我还没有从研究中找到一个好的示例。
编辑
我已经尝试使用 tarfile 模块进行以下操作,但不幸的是它不起作用,我认为因为 GZIP 文件不是使用 tar 创建的。
# get the zipped file's contents list, extract the file
with tarfile.TarFile(local_zipped_filename) as tar_file:
# list the contents, make sure we only extract the expected named file
members = tar_file.getmembers()
for member in members:
if member.name == filename_unzipped:
members_to_extract = [member]
tar_file.extractall(path=destination_dir, members=members_to_extract)
break # we've extracted our file, done
【问题讨论】:
标签: python python-3.x gzip