Read from stdouton Popen on WinXP?

jcarlson at uci.edu jcarlson at uci.edu
Thu Aug 18 01:01:06 EDT 2005


> >>> "mhenry1384" <mhenry1384 at gmail.com> 08/16/05 11:48 PM >>>
> I am trying to run a program and filter the output on Windows XP.
> Since I want to filter the output, I'd like to read it a line at a time
> and only print the lines I care about.
>
> p = subprocess.Popen(["doxygen.exe", r"Doxyfile.cfg"],
> stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

Install pywin32 .

Use:
  read(p.stdout, SIZE)
  read(p.stderr, SIZE)

Having defined read() as the below.  The subprocess generally still
needs to flush stdout and stderr.

 - Josiah


import msvcrt
import pywintypes
from win32file import ReadFile
from win32pipe import PeekNamedPipe

def read(stdout_or_stderr, maxsize):
    try:
        x = msvcrt.get_osfhandle(stdout_or_stderr.fileno())
        (read, nAvail, nMessage) = PeekNamedPipe(x, 0)
        if maxsize < nAvail:
            nAvail = maxsize
        if nAvail > 0:
            (errCode, read) = ReadFile(x, nAvail, None)
    except pywintypes.error, e:
        errno = e.args[0]
        if errno == 109:  # other end disconnected
            stdout_or_stderr.close()
        else:
            raise

    return read




More information about the Python-list mailing list