我有一个gz文件,如何解压缩该文件并将内容保存为python中的txt? 我已经导入了gzip
file_path = gzip.open(file_name, 'rb')
答案 0 :(得分:1)
打开第二个文件并写入该文件怎么样?
import gzip
with gzip.open('file.txt.gz', 'rb') as f, open('file.txt', 'w') as f_out:
f_out.write(f.read())
答案 1 :(得分:1)
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)