Question

Im trying to download a bz2 compressed tarfile and create a tarfile.TarFile object from it.

import MyModule

import StringIO
import tarfile

tardata = StringIO.StringIO()
tardata.write(MyModule.getBz2TarFileData())
tardata.seek(0)

tar = tarfile.open(fileobj = tardata, mode="r:bz2")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/tarfile.py", line 896, in open
    return func(name, filemode, fileobj)
  File "/usr/lib/python2.4/tarfile.py", line 987, in bz2open
    pre, ext = os.path.splitext(name)
  File "/usr/lib/python2.4/posixpath.py", line 92, in splitext
    i = p.rfind('.')
AttributeError: 'NoneType' object has no attribute 'rfind'

According to the docs (http://docs.python.org/library/tarfile.html#tarfile.open) when you use fileobj= it is used in favor of file name=. Though, it looks like its still trying to access a null file?

If fileobj is specified, it is used as an alternative to a file object opened for name. It is supposed to be at position 0.

If I don't use tarfile.open() and I decompress the bz2 data and create the tarfile.Tarfile object manually it works with StringIO and fileobj:

>>> import MyModule
>>> import tarfile
>>> import StringIO
>>> import bz2
>>> tardata = StringIO.StringIO()
>>> tardata.write(bz2.decompress(MyModule.getBz2TarFileData()))
>>> tardata.seek(0)
>>> tar = tarfile.TarFile(fileobj=tardata, mode='r')
>>> tar.getmembers()
[<TarInfo 'FileNumber1' at -0x48e150f4>, <TarInfo 'FileNumber2' at -0x48e150d4>, <TarInfo 'FileNumber3' at -0x48e11fb4>]
>>> 

I was trying to streamline since tarfile is supposed to support bz2 compression.

Was it helpful?

Solution

I just have thrown a look into tarfile.py on my systems. The line numbers were quite different (I have 2.6), so that I would suppose there was heavy work since 2.4.

Maybe the module had a bug in 2.4 times which has been corrected, or the said interface has changed thus the docs don't match your module version any longer.

It is just a guess, however.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top