Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 使用Python子流程模块将输入传递到可执行文件_Bash_Python 2.7_Subprocess - Fatal编程技术网

Bash 使用Python子流程模块将输入传递到可执行文件

Bash 使用Python子流程模块将输入传递到可执行文件,bash,python-2.7,subprocess,Bash,Python 2.7,Subprocess,我有一个名为0.in的输入文件。为了获得输出,我在bashshell中执行/a.out

我有一个名为
0.in
的输入文件。为了获得输出,我在bashshell中执行
/a.out<0.in

现在,我有几个这样的文件(超过500个),我想使用Python的子流程模块自动化这个过程

我试着这样做:

data=subprocess.Popen(['./a.out','< 0.in'],stdout=subprocess.PIPE,stdin=subprocess.PIPE).communicate()
data=subprocess.Popen(['./a.out','<0.in'],stdout=subprocess.PIPE,stdin=subprocess.PIPE).communicate()

运行此操作时未打印任何内容(数据[0]为空)。使用
@Rayu进行重定向的正确方法是什么?

由于第二种方法不会生成shell,因此我希望它更快,并且使用更少的内存。不要同时使用列表参数和
shell=True
,这会产生误导(总是传递字符串)。您可以使用
子流程。在此处检查输出(['./a.out'],stdin=f)
data = subprocess.Popen(['./a.out < 0.in'], stdout=subprocess.PIPE, shell=True).communicate()
with open('0.in') as f:
    data = subprocess.Popen(['./a.out'], stdout=subprocess.PIPE, stdin=f).communicate()