using tarfile with an open file object

Peter Hansen peter at engcorp.com
Wed May 4 16:55:36 EDT 2005


Matthew Thorley wrote:
> I've been using tarfile like this
> 
> import tarfile
> tar = tarfile.open('path_to_tar_archive', 'r:gz')
> 
> 
> But I need to use it like this:
> 
> archive = open('path_to_tar_archive', 'r')
> tar = tarfile.open(archive.readlines())
> 
> or something similar. In essence I need to use tarfile to manipulate an
> already open file object. I searched in the docs but didn't find
> anything. Maybe I just over looked the obvious.
> 
> Does any one know how to do this?

I don't know if this is in the docs (please check and submit a bug 
report if it's not), but the source shows that you should be able to do 
this:

tar = tarfile.open(fileobj=archive)

and carry on as you wish.

-Peter



More information about the Python-list mailing list