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)