【发布时间】:2014-02-26 15:19:30
【问题描述】:
我正在尝试使用 Python 来自动化涉及调用 Fortran 可执行文件和提交一些用户输入的过程。我花了几个小时阅读类似的问题并尝试不同的事情,但没有任何运气。这是一个显示我上次尝试的最小示例
#!/usr/bin/python
import subprocess
# Calling executable
ps = subprocess.Popen('fortranExecutable',shell=True,stdin=subprocess.PIPE)
ps.communicate('argument 1')
ps.communicate('argument 2')
但是,当我尝试运行它时,我收到以下错误:
File "gridGen.py", line 216, in <module>
ps.communicate(outputName)
File "/opt/apps/python/epd/7.2.2/lib/python2.7/subprocess.py", line 737, in communicate
self.stdin.write(input)
ValueError: I/O operation on closed file
非常感谢任何建议或指示。
编辑:
当我调用 Fortran 可执行文件时,它要求用户输入如下:
fortranExecutable
Enter name of input file: 'this is where I want to put argument 1'
Enter name of output file: 'this is where I want to put argument 2'
不知何故,我需要运行可执行文件,等待它要求用户输入,然后提供该输入。
【问题讨论】:
标签: python subprocess stdin communicate