Reputation: 6538
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.
Upvotes: 0
Views: 1267
Reputation: 399881
I don't see the problem. You should be able to use e.g. 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()
.
Upvotes: 3
Reputation: 840
If you want to compress a file without writing to it, you could run a shell command such as gzip
using the Python libraries subprocess
or popen
or os.system
.
Upvotes: 0