Iterating through 2 files simultaneously

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • glman74@gmail.com

    Iterating through 2 files simultaneously

    Hi folks,

    I am trying to tee off both stdout and stderr from a process run
    through Popen.
    As a test, I am first trying to print the output below:

    from subprocess import Popen,PIPE
    ....
    p1 = Popen(['cvs', 'update'], stdout=PIPE, stderr=PIPE)
    for (l1, l2) in zip(p1.stdout, p1.stderr):
    print '-->' + l1,
    print '-->' + l2,

    This doesn't work - probably because I cannot iterate through the
    pipes this way.

    I am new to Python, and I'd appreciate it if you could please explain
    why this
    doesn't work and/or suggest an alternate way to redirect stdout and
    stderr to a
    common place.

    My objective is for my code to print out stdout/stderr messages and at
    the same
    time redirect them to a log file.

    Thanks

    Mahesh
  • Rob Williscroft

    #2
    Re: Iterating through 2 files simultaneously

    wrote in news:7ae96aff-c1a7-4763-8db7-
    ff23766296df@u6 g2000prc.google groups.com in comp.lang.pytho n:
    Hi folks,
    >
    I am trying to tee off both stdout and stderr from a process run
    through Popen.
    As a test, I am first trying to print the output below:
    >
    from subprocess import Popen,PIPE
    ...
    p1 = Popen(['cvs', 'update'], stdout=PIPE, stderr=PIPE)
    for (l1, l2) in zip(p1.stdout, p1.stderr):
    print '-->' + l1,
    print '-->' + l2,
    >
    This doesn't work - probably because I cannot iterate through the
    pipes this way.
    >
    I am new to Python, and I'd appreciate it if you could please explain
    why this
    doesn't work and/or suggest an alternate way to redirect stdout and
    stderr to a
    common place.
    >
    My objective is for my code to print out stdout/stderr messages and at
    the same
    time redirect them to a log file.
    From the manual <url: http://docs.python.org/lib/node528.html>:

    stdin, stdout and stderr specify ...
    .... Additionally, stderr can be STDOUT, which indicates that the stderr
    data from the applications should be captured into the same file handle
    as for stdout.

    So import STDOUT and make stderr=STDOUT in the Popen call, you will then
    have one file/pipe to deal with p1.stdout.

    Rob.
    --

    Comment

    • glman74@gmail.com

      #3
      Re: Iterating through 2 files simultaneously

      >
      So import STDOUT and make stderr=STDOUT in the Popen call, you will then
      have one file/pipe to deal with p1.stdout.
      Thank you - that works great!

      Mahesh

      Comment

      Working...