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
What you want is popen, which makes it possible to directly read the output of a command like you would read a file:
import os
with os.popen('ps -ef') as pse:
for line in pse:
print line
# presumably parse line now
Note that, if you want more complex parsing, you'll have to dig into the documentation of subprocess.Popen.