[Tutor] using popen(n) to intercept stdout

Tim Johnson tim at johnsons-web.com
Thu Feb 16 06:46:19 CET 2006


Hey Hugo:

I now realize that to properly test this, I should use a command that
returns legitimate data:
I think the following gets me started:
f = os.popen('ls *.py','r').read()
now I have captured the output from 'ls *.py'.

So, if I do this with popen3:
>>> a,b,c = os.popen3('wt *.py','r')
>>> res = a.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IOError: [Errno 9] Bad file descriptor
>>> err = c.read()
>>> err
'/bin/sh: line 1: wt: command not found\n'

Then I can trap for IOError and hand either a legal response
or an error.

Hope I'm still on the right track.
thanks
tim

* Hugo González Monteverde <hugonz-lists at h-lab.net> [060215 20:25]:
> I also noticed that if you want to get the manpage, you will be hurt by 
> the interactivity of 'man' (it uses less for paging) One way to get the 
> whole manpage as text without paging is to do:
> 
> man -P /bin/cat
> 
> So you won't have to do strange stuff in stdin to get it to give you the 
> whole text.
> 
> Hugo

-- 
Tim Johnson <tim at johnsons-web.com>
      http://www.alaska-internet-solutions.com


More information about the Tutor mailing list