Reading the error stream from os.popen()

Tov Are Jacobsen tovj at stud.ntnu.no
Thu Oct 19 09:37:02 EDT 2000


I just made my first python script, and hit a problem.

The msgfmt command below outputs info to the stderr stream,
but the read() method retrieves data from stdin.

s = os.popen('/usr/bin/msgfmt  --statistics '+x[0]+'.po').read()

Any ideas? (I would like to make my script to work with an
unmodified version of msgfmt) :-)

#
# stat.py -- prints translation status
#
# Manual administration required, add/remove language code in the
# lang variable.
#
# Requires modified version of msgfmt which sends output to stdout.
#

import os
import re
import string

lang = [
  ['ca',    0.0, 'Catalan'],
  ['da',    0.0, 'Danish'],
  ['de',    0.0, 'German'],
  ['en_GB', 0.0, 'British'],
  ['es',    0.0, 'Spanish'],
  ['fi',    0.0, 'Finnish'],
  ['fr',    0.0, 'French'],
  ['ga',    0.0, 'Irish'],
  ['gl',    0.0, 'Galician'],
  ['it',    0.0, 'Italian'],
  ['ja',    0.0, 'Japanese'],
  ['ko',    0.0, 'Korean'],
  ['lt',    0.0, 'Lithuanian'],
  ['nl',    0.0, 'Dutch'],
  ['no',    0.0, 'Norwegian'],
  ['pl',    0.0, 'Polish'],
  ['ru',    0.0, 'Russian'],
  ['sl',    0.0, 'Slovenian'],
  ['sp',    0.0, 'Serbian (cyrrilic)'],
  ['sr',    0.0, 'Serbian (latin)'],
  ['sv',    0.0, 'Swedish'],
  ['tr',    0.0, 'Turkish'],
  ['uk',    0.0, 'Ukranian']]

reg = re.compile("(\d+)")

for x in lang:
   # update the .po files.
   os.popen('sh update.sh '+x[0])
   # retrieve statistics
   s = os.popen('/usr/bin/msgfmt  --statistics '+x[0]+'.po').read()
   all = reg.findall(s);
   n = len(all)
   p1 = 0.0
   p2 = 0.0
   p3 = 0.0
   if n >= 1:
      p1 = float(int(all[0]))
   if n >= 2:
      p2 = float(int(all[1]))
   if n >= 3:
      p3 = float(int(all[2]))
   tot = p1+p2+p3
   un = tot-(p2+p3)
   x[1] = (un/tot)*100

# Descending sort on percentage
lang.sort(lambda x, y: int((y[1]*10-x[1]*10)))

print """
PAN TRANSLATION STATUS

Debug messages are not translated in some languages.
en_GB is a minor variation on the english translation.
"""

i=1
for x in lang:
   print '%3d' % i, '%22s' % (x[2]+"("+x[0]+")"), '%6.1f' % x[1]
   i=i+1

#--------------------


--
Tov Are Jacobsen



More information about the Python-list mailing list