【发布时间】:2018-11-15 03:09:07
【问题描述】:
我有一个gz文件,如何解压文件并将内容保存到python中的txt? 我已经导入了 gzip
file_path = gzip.open(file_name, 'rb')
【问题讨论】:
-
您是否尝试过查看该对象现在可以使用哪些功能?
我有一个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 的 open 方法应该打开文件,使其内容可以像普通文件一样被读取:
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)
【讨论】: