bz2.decompress as file handle

Tim Chase python.list at tim.thechases.com
Sun May 18 22:32:56 EDT 2014


On 2014-05-18 19:53, Vincent Davis wrote:
> I have a file compressed with bz2 and a function that expects a
> file handle. When I decompress the bz2 file I get a string (binary)
> not a file handle.

> from bz2 import decompress,
> 
> with open('Tests/Affy/affy_v3_ex.CEL.bz2', 'rb') as handle:
>     cel_data = decompress(handle.read())

When I try (without the Bio.Affy which isn't part of the stdlib), I
get correct bytes from this:

tim at bigbox:~$ echo hello world > test.txt
tim at bigbox:~$ bzip2 -9 test.txt 
tim at bigbox:~$ python3
Python 3.2.3 (default, Feb 20 2013, 14:44:27) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from bz2 import decompress
>>> with open('test.txt.bz2', 'rb') as f:
...     data = decompress(f.read())
... 
>>> data
b'hello world\n'


> c = CelFile.read(cel_data)

So either you have bad data in the file to begin with, or your
CelFile.read() function has a bug in it.

-tkc






More information about the Python-list mailing list