[Tutor] using tarfile on strings or filelike objects

Kent Johnson kent37 at tds.net
Mon Mar 5 13:44:10 CET 2007


Barton David wrote:
> Thanks Kent,
> 
> But.. I've actually found that that doesn't work (raises a ReadError),
> and neither does..
>>>> tf=tarfile.open(mode="r|*",fileobj=filelike)
> ..which raises "AttributeError: _Stream instance has no attribute
> 'dbuf'"
> 
> However if I explicitly state the compression type.. e.g.
>>>> tf=tarfile.open(mode="r|bz2",fileobj=filelike)
> ..then I can indeed go on to..
>>>> print tf.getnames()
>>>> assert archive_member_name in tf.getnames()
> ..and it works ok. Having to explicitly state the compression type isn't
> exactly ideal, but I guess it'll do me for now.
> 
> Unfortunately, I'm still having trouble actually reading the contents of
> 'archive_member'.
> i.e. ..
>>>> tf_filelike=tf.extractfile(archive_member_name)
>>>> print tf_filelike.read()
> ..raises..
> File "C:\Python24\lib\tarfile.py", line 551, in _readnormal
>     self.fileobj.seek(self.offset + self.pos)
> File "C:\Python24\lib\tarfile.py", line 420, in seek
>     raise StreamError, "seeking backwards is not allowed"

The docs for the | options say that seeking won't work with them. Maybe 
try just 'r'? A hacky workaround might be to open the file once to get 
the names out and a second time to read the data. Since the file is in 
memory that should be pretty quick.
> 
> And I get the same error if I instead try..
>>>> tf_infoobject=tf.getmember(archive_member_name)
>>>> tf_filelike=tf.extractfile(tf_infoobject)
>>>> print tf_filelike.read()
> 
> In fact I'm getting this even if I open the archive by passing the path
> name (rather than using fileobj) so I guess this isn't the problem I
> initially thought it was. I just don't get it.

What if you try exactly the code shown in the tarfile examples for 
reading from stdin, with your StringIO file substituted for stdin?

You might want to ask about this on comp.lang.python as no one here 
seems to know the answer. This thread is slightly related:
http://tinyurl.com/2hrffs

Kent

> 
> Regards,
> Dave


More information about the Tutor mailing list