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()