IOError: (0,"Error") when closing "os.popen"ed file?

Rob W. W. Hooft rob at hooft.net
Mon Jan 31 08:08:01 EST 2000


In my project tools, I have a routine "myopen" with the following
code:

compresstable={'.Z':'compress','.gz':'gzip','.bz2':'bzip2'}

def myopen(filename,mode):
    """Open a file. Based on the filename extension, open it through different
       compression/decompression program pipes.

       parameters:
           filename : name of file to open.
           mode : 'r' or 'w' for read-only or write-only open.
    """
    from interface import compress
    if mode=='r':
        for ext,prog in compresstable.items():
            if filename[-len(ext):]==ext:
                return os.popen(prog+' -dc '+filename+' 2> /dev/null' ,'r')
        else:
            return open(filename,'r')
    elif mode=='w':
        for ext,prog in compresstable.items():
            if filename[-len(ext):]==ext:
                return os.popen(prog+' > '+filename,'w')
        else:
            return open(filename,'w')
    raise IOError("unknown filemode?")

Now I can use 
 
   f=projtls.myopen("file.Z","w")

to write compressed data to disk: myopen will open the file as:
 
   f=os.popen('compress > file.Z','w')

This approach has worked until a few weeks ago..... Now we have a new
fast computer running RedHat linux 6.1, and a 450MHz PIII processor.
Suddenly, I started getting strange exceptions from "f.close()" statements
for these "popen"ed files:

   IOError: (0,"Error")

When such an error on close occurs, most of the time the last few kB of
data are missing from the compressed output file. 

After hacking for a while, I managed to work around the problem by
doing:

    time.sleep(0.1)
    f.close()

But this is highly unsatisfactory..... Is there anyone that can give me 
a better suggestion?

Regards,

Rob Hooft
-- 
=====   rob at hooft.net          http://www.xs4all.nl/~hooft/rob/  =====
=====   R&D, Nonius BV, Delft  http://www.nonius.nl/             =====
===== PGPid 0xFA19277D ========================== Use Linux! =========



More information about the Python-list mailing list