getting output from command that isn't STDIN

Donn Cave donn at u.washington.edu
Wed Feb 26 12:26:55 EST 2003


Quoth "ldu02thc" <ldu02thc at rdg.ac.uk>:
| I need to get the output from mencoder as it encodes videos. Most of it can
| be retrieved easily enough, but after the initial blurb, it then displays a
| progress meter with various information including percentage complete, and
| that stays on the same line, if you get me, like wget's progress meter.
|
| That line doesn't get picked up through STDTIN or STDERR... I only get the
| last time that line is refreshed (when it's done) which isn't much use. How
| can I grab this output?
|
| The code atm is like this:
| pipe = popen(command)
| while 1:
|         line = pipe.readline()
|         if line == '':
|                 break
|         print line

Simple - your application isn't generating a line there, so readline()
doesn't complete.  A line is '[^\n]*\n', that is, 0 or more bytes
ending with the line feed character variously represented as '\n',
octal 012, LF etc.  Its effect in a terminal emulator is to advance
down one line (plus normally an implicit carriage return), and your
application doesn't want that, it wants to keep updating the same
line on the screen, so it omits the line feed and probably supplies
an explicit carriage return.  No line feeds, therefore no lines.

Anyway, you could probably get data with a read() function, or even
work directly with the pipe file descriptor, like

  pipe = popen(command, 'r').fileno()
  while 1:
      data = os.read(pipe, 3000)
      if not data:
          break
      os.write(1, data)

	Donn Cave, donn at u.washington.edu




More information about the Python-list mailing list