[Tutor] getting the size of an open TarFile object

Kent Johnson kent37 at tds.net
Sun Aug 19 13:39:43 CEST 2007

Adam A. Zajac wrote:
> Hello all,
> I've been working with the tarfile module and just noticed an apparent
> flaw in my own logic.  I wanted to add files to the tar until it neared
> a certain file size.  After every addition I would use
> os.path.getsize().  What I noticed today is that I could add a file to
> the tar without the tar's size changing.  I'm assuming that because the
> TarFile object is not closed, finding its size that way isn't reliable.
> The first thing I tried was using flush(), but TarFile objects
> apparently don't do that.  I also can't close the object and reopen it
> for appending because TarFile objects can't append if the tar is
> compressed.  And finally, I can't predict the compression ratio, so I
> can't merely keep track of the size of the individual files without a
> huge gap to my target size.
> I'm giving up on it for the night.  Anyone have any thoughts?

Something to try, just from looking at the docs and the code:

You can open the output file yourself and pass it to tarfile.open(). 
Then you could use f.tell() to get an idea how many bytes have been 
written. The file-like object being used by the tarfile is stored in its 
fileobj attribute so maybe you can flush that before the tell().


More information about the Tutor mailing list