Everypne else has answered pretty muh about this. I just want to add that if you want to read noncanonically (less thana line ending in "\n" you'll have to do it char by char =( AFAIK, there's no way to redefine a separator por readlines() (other than \n..)

Hugo

Nick Lunt wrote:
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:

[code]
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,
[/code]

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 .


_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to