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

Erik Bray embray at stsci.edu
Mon Sep 9 19:02:37 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

I was just taking another look at this because I've been working on refactoring 
some of the file handling code in pyfits.  And one thing that I noticed is that 
the `writeto()` method was only ever really designed for writing out a *new* 
file, and doesn't support appending to an existing file.  Now, it should still 
accept a file with 'ab+' mode and just write a new file (so long as a file with 
the given name doesn't already exist).  But in the case that the file doesn't 
already exist there's no reason to use append mode.  Just 'wb' should be fine.

Erik

> # 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)
>
>
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> http://mail.scipy.org/mailman/listinfo/astropy
>




More information about the AstroPy mailing list