简体   繁体   中英

How to compress a processed text file in Python?

I have a text file which I constantly append data to. When processing is done I need to gzip the file. I tried several options like shutil.make_archive , tarfile , gzip but could not eventually do it. Is there no simple way to compress a file without actually writing to it?

Let's say I have mydata.txt file and I want it to be gzipped and saved as mydata.txt.gz .

I don't see the problem. You should be able to use eg the gzip module just fine, something like this:

inf = open("mydata.txt", "rb")
outf = gzip.open("file.txt.gz", "wb")
outf.write(inf.read())
outf.close()
inf.close()

There's no problem with the file being overwritten, the name given to gzip.open() is completely independent of the name given to plain open() .

如果要压缩文件而不写入文件,则可以使用Python库subprocess os.systempopenos.system运行shell命令,例如gzip

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM