簡體   English   中英

如何打開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的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)

暫無
暫無

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

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