[Tutor] getting the size of an open TarFile object

Adam A. Zajac strider1551 at gmail.com
Tue Aug 21 17:52:08 CEST 2007


> > 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().
> 
> HTH,
> Kent

That's a brilliant thought.  I played with it, and it does seem to give
me a more accurate report;  in the end, though, it's still not exact.
Oh well.  For now I'm just putting in a "buffer" and assuming that the
archive is 1M larger than whatever it tells me.  That seems to keep it
from going over at the cost of having a little space that could have
been used otherwise.


More information about the Tutor mailing list