os.popen and broken pipes

Donn Cave donn at u.washington.edu
Fri Feb 9 15:47:32 EST 2007


In article <eqhpnn$573$1 at news.lrz-muenchen.de>,
 Philipp Pagel <pDOTpagel at gsf.de> wrote:

> Antoon Pardon <apardon at forel.vub.ac.be> wrote:
> > On 2007-02-09, Philipp Pagel <pDOTpagel at gsf.de> wrote:
> > > for filename in file_list:
> > >     file = os.popen('uncompress -c '+filename, 'r')
> > >     do_something(file)
> > >     file.close()
> > >
> > > This works fine for some files but results in
> > >
> > > 'write error onstdout: Broken pipe'
> 
> > As far as I can tell, your do_something doesn't consume the entire file.
> > So you close the file prematurly, which results in the uncompress/zcat
> > program trying to write to a pipe that is closed on the otherside,
> > giving you the above message.
> 
> You are right: some of the files do not fulfill certain
> critereia causing so_somehting() to return before the entire file is
> processed.

Most programming environments don't have this problem, though.

If you like, your program can undo what Python does:

    signal.signal(signal.SIGPIPE, signal.SIG_DFL)
    for filename in file_list:
        ...

Then it will work as if you had written it in C, or awk
or whatever.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list