如何打开 gz 文件并将文件另存为 python 中的 txt

how to open a gz file and save the file as txt in python

我有一个 gz 文件,如何解压缩文件并将内容保存到 python 中的 txt? 我已经导入了 gzip

file_path = gzip.open(file_name, 'rb')

打开第二个文件并写入如何?

import gzip
with gzip.open('file.txt.gz', 'rb') as f, open('file.txt', 'w') as f_out:
    f_out.write(f.read())

Gzip 的打开方法应该以可以像普通文件一样读取其内容的方式打开文件:

import gzip

#Define the file's location
file_path = "/path/to/file.gz"

#Open the file and read its contents
with gzip.open(file_path, "rb") as file:
    file_content = file.read()


#Save the new txt file
txt_file_name = "txtFile.txt"

with open(txt_file_name, "w") as file:
    file.write(file_content)