[英]Python Subprocess : Send a sequence of commands to a process depending on the output of the previous command
I am trying to execute a sequence of commands with the program xfoil .我正在尝试使用程序xfoil执行一系列命令。
I've got to the point of getting all the commands I'm sending so that it can be directly loaded without control to xfoil with input redirection ex : "xfoil < inputfile"我已经到了获取我发送的所有命令的地步,以便它可以直接加载而无需控制输入重定向到 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
With subprocess or some other means, is there a way for me to send one command at a time, such that the output of the first load command is checked before sending the second?使用子进程或其他方式,有没有办法让我一次发送一个命令,以便在发送第二个命令之前检查第一个加载命令的输出?
preferably with something similar to:最好有类似的东西:
ps = sp.Popen(['xfoil.exe'], stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
for command in commdand_list:
ps.stdin.write(cmd+'\n')
You can try the following:您可以尝试以下操作:
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}")
if you want to fail the loop if a specific command failed, you can add check=True
as a parameter to subprocess.run(...,check=True)
function.如果您想在特定命令失败时使循环失败,您可以将
check=True
作为参数添加到subprocess.run(...,check=True)
函数。
In addition, if xfoil
is runnable only from specific directory, you can cwd="path/to/run"
to subprocess.run()
.此外,如果
xfoil
只能从特定目录运行,您可以cwd="path/to/run"
到subprocess.run()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.