redirecting output of process to a file using subprocess.Popen()

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

    #1

    redirecting output of process to a file using subprocess.Popen()

    I am trying to redirect stderr of a process to a temporary file and
    then read back the contents of the file, all in the same python
    script. As a simple exercise, I launched /bin/ls but this doesn't
    work:

    #!/usr/bin/python
    import subprocess as proc
    import tempfile
    name = tempfile.NamedT emporaryFile(mo de='w+b')
    print 'name is '+ name.name

    cmd = []
    cmd.append('/bin/ls')
    cmd.append('-l')
    cmd.append('/tmp')
    p = proc.Popen(cmd, stdout=name, stderr=proc.STD OUT, close_fds=True)
    while True:
    ret = p.poll()
    if (ret is not None):
    output = name.readlines( )
    print 'out = ', output
    break

    $python sub.py
    name is /tmp/tmpjz4NJY
    out = []


    I tried calling flush() on the file object but this didn't help
    either. Tried closing and re-opening the file, but closing the file
    object results in it getting deleted. Can the above be made to work by
    using tempfiles?

    thanks
  • skeept

    #2
    Re: redirecting output of process to a file using subprocess.Pope n()

    On Jul 9, 7:32 pm, rpar...@gmail.c om wrote:
    I am trying to redirect stderr of a process to a temporary file and
    then read back the contents of the file, all in the same python
    script. As a simple exercise, I launched /bin/ls but this doesn't
    work:
    >
    #!/usr/bin/python
    import subprocess as proc
    import tempfile
    name = tempfile.NamedT emporaryFile(mo de='w+b')
    print 'name is '+ name.name
    >
    cmd = []
    cmd.append('/bin/ls')
    cmd.append('-l')
    cmd.append('/tmp')
    p = proc.Popen(cmd, stdout=name, stderr=proc.STD OUT, close_fds=True)
    while True:
       ret = p.poll()
       if (ret is not None):
          output = name.readlines( )
          print 'out = ', output
          break
    >
    $python sub.py
    name is /tmp/tmpjz4NJY
    out =  []
    >
    I tried calling flush() on the file object but this didn't help
    either. Tried closing and re-opening the file, but closing the file
    object results in it getting deleted. Can the above be made to work by
    using tempfiles?
    >
    thanks

    your script works just fine.
    The problem is that you have to move to the beggining of the file to
    read the actual contents of
    the file.
    name.seek(0)

    The whole program would be:

    #!/usr/bin/python
    import subprocess as proc
    import tempfile
    name = tempfile.NamedT emporaryFile(mo de='w+b')
    print 'name is '+ name.name

    cmd = []
    cmd.append('/bin/ls')
    cmd.append('-l')
    cmd.append('/tmp')
    p = proc.Popen(cmd, stdout=name, stderr=proc.STD OUT, close_fds=True)
    while True:
    ret = p.poll()
    if (ret is not None):
    name.seek(0)
    output = name.readlines( )
    print 'out = ', output
    break

    Comment

    • Matt Nordhoff

      #3
      Re: redirecting output of process to a file using subprocess.Pope n()

      skeept wrote:
      On Jul 9, 7:32 pm, rpar...@gmail.c om wrote:
      >I am trying to redirect stderr of a process to a temporary file and
      >then read back the contents of the file, all in the same python
      >script. As a simple exercise, I launched /bin/ls but this doesn't
      >work:
      >>
      >#!/usr/bin/python
      >import subprocess as proc
      >import tempfile
      >name = tempfile.NamedT emporaryFile(mo de='w+b')
      >print 'name is '+ name.name
      >>
      >cmd = []
      >cmd.append('/bin/ls')
      >cmd.append('-l')
      >cmd.append('/tmp')
      >p = proc.Popen(cmd, stdout=name, stderr=proc.STD OUT, close_fds=True)
      >while True:
      > ret = p.poll()
      > if (ret is not None):
      > output = name.readlines( )
      > print 'out = ', output
      > break
      >>
      >$python sub.py
      >name is /tmp/tmpjz4NJY
      >out = []
      >>
      >I tried calling flush() on the file object but this didn't help
      >either. Tried closing and re-opening the file, but closing the file
      >object results in it getting deleted. Can the above be made to work by
      >using tempfiles?
      >>
      >thanks
      >
      >
      your script works just fine.
      The problem is that you have to move to the beggining of the file to
      read the actual contents of
      the file.
      name.seek(0)
      >
      The whole program would be:
      >
      #!/usr/bin/python
      import subprocess as proc
      import tempfile
      name = tempfile.NamedT emporaryFile(mo de='w+b')
      print 'name is '+ name.name
      >
      cmd = []
      cmd.append('/bin/ls')
      cmd.append('-l')
      cmd.append('/tmp')
      p = proc.Popen(cmd, stdout=name, stderr=proc.STD OUT, close_fds=True)
      while True:
      ret = p.poll()
      if (ret is not None):
      name.seek(0)
      output = name.readlines( )
      print 'out = ', output
      break
      This is an aside, but why the loop?

      ....
      p = ...
      p.wait()
      name.seek(0)
      ....

      (p.wait() returns p.returncode, just like p.poll() does, but you aren't
      using it anyway...)
      --

      Comment

      Working...