Numpy array to gzip file

drobinow at drobinow at
Wed Jun 11 18:42:33 CEST 2008

On Jun 11, 9:17 am, Sean Davis <seand... at> wrote:
> I have a set of numpy arrays which I would like to save to a gzip
> file.  Here is an example without gzip:
> b=numpy.ones(1000000,dtype=numpy.uint8)
> a=numpy.zeros(1000000,dtype=numpy.uint8)
> fd = file('test.dat','wb')
> a.tofile(fd)
> b.tofile(fd)
> fd.close()
> This works fine.  However, this does not:
> fd ='test.dat','wb')
> a.tofile(fd)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> IOError: first argument must be a string or open file
> In the bigger picture, I want to be able to write multiple numpy
> arrays with some metadata to a binary file for very fast reading, and
> these arrays are pretty compressible (strings of small integers), so I
> can probably benefit in speed and file size by gzipping.
> Thanks,
> Sean


The documentation says that gzip simulates most of the methods of a
file object.
Apparently that means it does not subclass it.  numpy.tofile wants a
file object
 Or something like that.

More information about the Python-list mailing list