[AstroPy] pyfits does not write to gzip ab+ objects

Erik Bray embray at stsci.edu
Tue Aug 27 11:49:08 EDT 2013


On 08/23/2013 01:49 AM, John K. Parejko wrote:
> Hello,
>
> On python2.7/pyfits3.1.2 , when I try to write to a gzip object in append mode, 'ab+', I receive an IOError: [Errno 9] read() on write-only GzipFile object. Whereas when I use mode 'ab', I receive no error.
>
> The former worked in pyfits 2.4-ish (what we were previously using). We use NamedTemporaryFiles to prevent accidental filename collision, and pass that through a gzip object to select the compression level, before writing with hdu.writeto(tempFile,checksum=True).
>
> I'm not sure whether pyfits or gzip is really the culprit here. I've attached an example.
>
> Thanks in advance,
> John
>
> # Example:
>
> import gzip
> import pyfits
> import numpy as np
>
> hdu=pyfits.PrimaryHDU(np.random.random(10))
>
> #IOError: [Errno 9] read() on write-only GzipFile object
> f=gzip.GzipFile(filename='foo.fits.gz',mode='ab+',compresslevel=4)
> hdu.writeto(f)
>
> #works, resulting file is gzipped.
> f=gzip.GzipFile(filename='foo.fits.gz',mode='ab',compresslevel=4)
> hdu.writeto(f)

I think that the gzip module does not actually support 'ab+' mode, and that if 
you pass that in it gets confused though it doesn't actually give you any useful 
errors up front.  Essentially 'ab' is a write-only mode, but 'ab+' is 
read/write, so when you pass in 'ab+' it opens the underlying file object in a 
read/write mode but the GzipFile itself is write-only.  In think somewhere this 
incongruity causes problems...

In this case if you're just writing a new file 'wb' should suffice too.

Erik



More information about the AstroPy mailing list