我的
Python脚本中有一个标志,指定我是否设置和使用外部进程.此过程是一个名为my_command的命令,它从标准输入中获取数据.如果我在命令行上运行它,它将是这样的:
$my_command < data > result
我想使用Python脚本通过修改标准输入并将其提供给my_command来生成数据行.
我正在做这样的事情:
import getopt, sys, os, stat, subprocess
# for argument's sake, let's say this is set to True for now
# in real life, I use getopt.getopt() to decide whether this is True or False
useProcess = True
if useProcess:
process = subprocess.Popen(['my_command'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
for line in sys.stdin:
# parse line from standard input and modify it
# we store the result in a variable called modified_line
modified_line = line + "foo"
# if we want to feed modified_line to my_command, do the following:
if useProcess:
process.stdin.write(modified_line)
# otherwise, we just print the modifie