Skip to content Skip to sidebar Skip to footer

Read From Stdin AND Forward It To A Subprocess In Python

I'm writing a wrapper script for a program that optionally accepts input from STDIN. My script needs to process each line of the file, but it also needs to forward STDIN to the pro

Solution 1:

Does this work for you?

#!/usr/bin/env python2
import subprocess
import sys

p = subprocess.Popen(['cat'], stdin = subprocess.PIPE)

line = sys.stdin.readline()

####################
# Insert work here #
####################

line = line.upper()

####################

p.communicate(line)

Example:

$ echo "hello world" | ./wrapper.py 
HELLO WORLD

Post a Comment for "Read From Stdin AND Forward It To A Subprocess In Python"