开发者

Python TarFile with bz2 data

开发者 https://www.devze.com 2023-03-31 16:27 出处:网络
Im trying to download a bz2 compressed tarfile and create a tarfile.TarFile object from it. import MyModule

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>, <Ta开发者_JAVA技巧rInfo 'FileNumber3' at -0x48e11fb4>]
>>> 

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消