[Tutor] using tarfile on strings or filelike objects

Barton David David.Barton at nottingham.ac.uk
Mon Mar 5 10:19:40 CET 2007


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"

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.

Regards,
Dave

-----Original Message-----
From: Kent Johnson [mailto:kent37 at tds.net] 
Sent: 03 March 2007 22:26
To: Barton David
Cc: tutor at python.org
Subject: Re: [Tutor] using tarfile on strings or filelike objects

Barton David wrote:
> I like that I can access the contents of a zip archive that's stored 
> in memory (rather than on disk) by packing the archive contents into a

> StringIO or cStringIO object and feeding that to ZipFile...
>  
> i.e.
>  
> filelike=cStringIO.StringIO(archive_as_string)
> zf=zipfile.ZipFile(filelike)
> content=zf.read(archive_member_name)
> zf.close()
> filelike.close()
>  
> but I can't get the same thing to work with TarFile. Is there any way 
> to do this? (Other than first saving the archive data to disk and then

> passing the path to TarFile.open?) The tarfile module documentation 
> talks about an optional fileobj flag but this doesn't seem to work.

Just from reading the docs I would try
filelike = cStringIO...
tf = tarfile.open(mode='r|', fileobj=filelike)

This is based on the example in the docs of reading from stdin.

Kent


This message has been checked for viruses but the contents of an attachment
may still contain software viruses, which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.



More information about the Tutor mailing list