Python中的Stdin重定向

Python中的Stdin重定向,python,io-redirection,Python,Io Redirection,假设我有一个名为some_binary的程序,它可以将数据读取为: some_binary < input 要在Python中模拟类似的内容,我有: import numpy as np # Random binary numbers first_column = np.random.random_integers(0,1, (6,)) # Random numbers between 0 and 1 second_column = np.random.random((6,)) 如

假设我有一个名为
some_binary
的程序,它可以将数据读取为:

some_binary < input
要在Python中模拟类似的内容,我有:

import numpy as np

# Random binary numbers
first_column = np.random.random_integers(0,1, (6,))

# Random numbers between 0 and 1
second_column = np.random.random((6,))
如何将
first_column
second_column
的串联馈送到
some_binary
,就像我从命令行调用
some_binary
一样,并在字符串中收集
stdout

我有以下资料:

def run_shell_command(cmd,cwd=None,my_input):
      retVal = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stdin=my_input, cwd=cwd);
      retVal = retVal.stdout.read().strip('\n');
      return(retVal);

但是我不确定我的方向是否正确。

是的,你的方向是正确的

您可以使用pythons
subprocess.check\u output()
函数,它是
subprocess.Popen()的方便包装。
Popen
需要更多的基础设施。例如,您需要在
Popen
的返回值上调用
comminucate()
,以使事情发生

差不多

output = subprocess.check_output([cmd], stdin = my_input)

对你来说应该行。

谢谢。如何从
第一列
第二列
构建
我的输入
,以达到预期效果?
output = subprocess.check_output([cmd], stdin = my_input)