[Tutor] reading from stdin

Nick Lunt nick at javacat.f2s.com
Tue Mar 1 23:08:00 CET 2005

Hi folks,

I've been pondering how to get python to read from a pipe outside of
itself, sort of.

For example I tried a simple python prog to do a grep, eg

# ps -e | myprog.py cron

would give this output

3778 ?        00:00:00 crond

same as 
# ps -e | grep cron

The way I did this was to use sys.stdin.readlines() to get the output
from the pipe.

Here is the program:

import sys, glob
args = sys.stdin.readlines() # found on the net
pat = sys.argv[1]
for i in args:
        if (i.find(pat) != -1):
                print i,

My question is am I getting the output from the pipe in the correct
way ? The way Im doing it works (so far) but should I be doing it
another way ?

Many thanks
Nick .

More information about the Tutor mailing list