Using Python to run executable and fill in user input

后端 未结 4 1238
太阳男子
太阳男子 2020-12-15 12:25

I\'m trying to use Python to automate a process that involves calling a Fortran executable and submitting some user inputs. I\'ve spent a few hours reading through similar

4条回答
  •  孤城傲影
    2020-12-15 13:18

    If the input doesn't depend on the previous answers then you could pass them all at once using .communicate():

    import os
    from subprocess import Popen, PIPE
    
    p = Popen('fortranExecutable', stdin=PIPE) #NOTE: no shell=True here
    p.communicate(os.linesep.join(["input 1", "input 2"]))
    

    .communicate() waits for process to terminate therefore you may call it at most once.

提交回复
热议问题