How to pipe input to python line by line from linux program?

CodeBlue picture CodeBlue · Jul 15, 2013 · Viewed 86.4k times · Source

I want to pipe the output of ps -ef to python line by line.

The script I am using is this (first.py) -

#! /usr/bin/python

import sys

for line in sys.argv:
   print line

Unfortunately, the "line" is split into words separated by whitespace. So, for example, if I do

echo "days go by and still" | xargs first.py

the output I get is

./first.py
days
go
by
and
still

How to write the script such that the output is

./first.py
days go by and still

?

Answer

Dr. Jan-Philip Gehrcke picture Dr. Jan-Philip Gehrcke · Jul 15, 2013

Instead of using command line arguments I suggest reading from standard input (stdin). Python has a simple idiom for iterating over lines at stdin:

import sys

for line in sys.stdin:
    sys.stdout.write(line)

My usage example (with above's code saved to iterate-stdin.py):

$ echo -e "first line\nsecond line" | python iterate-stdin.py 
first line
second line

With your example:

$ echo "days go by and still" | python iterate-stdin.py
days go by and still