Read from stdin AND forward it to a subprocess in Python

两盒软妹~` 提交于 2020-01-05 05:43:06

问题


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 program it is wrapping. In minimalist form, this looks something like this:

import subprocess
import sys

for line in sys.stdin:
    # Do something with each line
    pass

subprocess.call(['cat'])

Note that I'm not actually trying to wrap cat, it just serves as an example to demonstrate whether or not STDIN is being forwarded properly.

With the example above, if I comment out the for-loop, it works properly. But if I run it with the for-loop, nothing gets forwarded because I've already read to the end of STDIN. I can't seek(0) to the start of the file because you can't seek on streams.

One possible solution is to read the entire file into memory:

import subprocess
import sys

lines = sys.stdin.readlines()
for line in lines:
    # Do something with each line
    pass

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

which works, but isn't very memory efficient. Can anyone think of a better solution? Perhaps a way to split or copy the stream?

Additional Constraints:

  1. The subprocess can only be called once. So I can't read a line at a time, process it, and forward it to the subprocess.
  2. The solution must work in Python 2.6

回答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


来源:https://stackoverflow.com/questions/40935208/read-from-stdin-and-forward-it-to-a-subprocess-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!