I need to produce the tar.gzipped text file. Is there a way to create a file for constant writing (to be able to do something like compressedFile.write("some text")
), or do I need to create a raw text file first, and compress it aftewards?
This will be quite unfortunate, as the file should be really long and well compressable.
Here's an example of how to write a compressed tarfile from a Python script:
import StringIO
import tarfile
tar = tarfile.open('example.tar.gz', 'w:gz')
# create a file record
data = StringIO.StringIO('this is some text')
info = tar.tarinfo()
info.name = 'foo.txt'
info.uname = 'pat'
info.gname = 'users'
info.size = data.len
# add the file to the tar and close it
tar.addfile(info, data)
tar.close()
Result:
% tar tvf example.tar.gz
-rw-r--r-- 0 pat users 17 Dec 31 1969 foo.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With