[英]Python Subprocess : Send a sequence of commands to a process depending on the output of the previous command
我正在尝试使用程序xfoil执行一系列命令。
我已经到了获取我发送的所有命令的地步,以便它可以直接加载而无需控制输入重定向到 xfoil,例如:“xfoil < inputfile”
load sd7032.dat
oper
iter 100
type 2
visc 100000
alfa 0.0
dump target_sd7032_alfa_0.0_Re_100000_Type2.dmp
使用子进程或其他方式,有没有办法让我一次发送一个命令,以便在发送第二个命令之前检查第一个加载命令的输出?
最好有类似的东西:
ps = sp.Popen(['xfoil.exe'], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
for command in commdand_list:
ps.stdin.write(cmd+'\n')
您可以尝试以下操作:
import subprocess
for command in command_list:
res = subprocess.run(f'xfoil {command}', shell=True, capture_output=True)
print(f"return code is: {res.returncode}")
print(f"stdout is: {res.stdout}")
print(f"stderr is: {res.stderr}")
如果您想在特定命令失败时使循环失败,您可以将check=True
作为参数添加到subprocess.run(...,check=True)
函数。
此外,如果xfoil
只能从特定目录运行,您可以cwd="path/to/run"
到subprocess.run()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.