<tfoot id='eMPq8'></tfoot>

    <small id='eMPq8'></small><noframes id='eMPq8'>

    • <bdo id='eMPq8'></bdo><ul id='eMPq8'></ul>

        <i id='eMPq8'><tr id='eMPq8'><dt id='eMPq8'><q id='eMPq8'><span id='eMPq8'><b id='eMPq8'><form id='eMPq8'><ins id='eMPq8'></ins><ul id='eMPq8'></ul><sub id='eMPq8'></sub></form><legend id='eMPq8'></legend><bdo id='eMPq8'><pre id='eMPq8'><center id='eMPq8'></center></pre></bdo></b><th id='eMPq8'></th></span></q></dt></tr></i><div id='eMPq8'><tfoot id='eMPq8'></tfoot><dl id='eMPq8'><fieldset id='eMPq8'></fieldset></dl></div>
        <legend id='eMPq8'><style id='eMPq8'><dir id='eMPq8'><q id='eMPq8'></q></dir></style></legend>

        使用 Python 运行可执行文件并填写用户输入

        时间:2023-07-21
            <bdo id='HtMRl'></bdo><ul id='HtMRl'></ul>
              <tbody id='HtMRl'></tbody>
            <tfoot id='HtMRl'></tfoot>
          • <legend id='HtMRl'><style id='HtMRl'><dir id='HtMRl'><q id='HtMRl'></q></dir></style></legend>

              <small id='HtMRl'></small><noframes id='HtMRl'>

                  <i id='HtMRl'><tr id='HtMRl'><dt id='HtMRl'><q id='HtMRl'><span id='HtMRl'><b id='HtMRl'><form id='HtMRl'><ins id='HtMRl'></ins><ul id='HtMRl'></ul><sub id='HtMRl'></sub></form><legend id='HtMRl'></legend><bdo id='HtMRl'><pre id='HtMRl'><center id='HtMRl'></center></pre></bdo></b><th id='HtMRl'></th></span></q></dt></tr></i><div id='HtMRl'><tfoot id='HtMRl'></tfoot><dl id='HtMRl'><fieldset id='HtMRl'></fieldset></dl></div>
                  本文介绍了使用 Python 运行可执行文件并填写用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Python 来自动化涉及调用 Fortran 可执行文件和提交一些用户输入的过程.我花了几个小时阅读类似的问题并尝试不同的事情,但没有任何运气.这是一个显示我上次尝试的最小示例

                  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 questions and trying different things, but haven't had any luck. Here is a minimal example to show what I tried last

                  #!/usr/bin/python
                  
                  import subprocess
                  
                  # Calling executable 
                  ps = subprocess.Popen('fortranExecutable',shell=True,stdin=subprocess.PIPE)
                  ps.communicate('argument 1')
                  ps.communicate('argument 2')
                  

                  但是,当我尝试运行它时,我收到以下错误:

                  However, when I try to run this, I get the following error:

                    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
                  

                  非常感谢任何建议或指示.

                  Any suggestions or pointers are greatly appreciated.

                  当我调用 Fortran 可执行文件时,它要求用户输入如下:

                  When I call the Fortran executable, it asks for user input as follows:

                  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'
                  

                  不知何故,我需要运行可执行文件,等待它要求用户输入,然后提供该输入.

                  Somehow, I need to run the executable, wait until it asks for user input and then supply that input.

                  推荐答案

                  如果输入不依赖于先前的答案,那么您可以使用 .communicate() 一次将它们全部传递:

                  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() 等待进程终止,因此您最多可以调用一次.

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

                  这篇关于使用 Python 运行可执行文件并填写用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在后台执行子进程 下一篇:Python 子进程参数

                  相关文章

                    1. <legend id='OUMVD'><style id='OUMVD'><dir id='OUMVD'><q id='OUMVD'></q></dir></style></legend>

                        <bdo id='OUMVD'></bdo><ul id='OUMVD'></ul>
                      <tfoot id='OUMVD'></tfoot>
                    2. <i id='OUMVD'><tr id='OUMVD'><dt id='OUMVD'><q id='OUMVD'><span id='OUMVD'><b id='OUMVD'><form id='OUMVD'><ins id='OUMVD'></ins><ul id='OUMVD'></ul><sub id='OUMVD'></sub></form><legend id='OUMVD'></legend><bdo id='OUMVD'><pre id='OUMVD'><center id='OUMVD'></center></pre></bdo></b><th id='OUMVD'></th></span></q></dt></tr></i><div id='OUMVD'><tfoot id='OUMVD'></tfoot><dl id='OUMVD'><fieldset id='OUMVD'></fieldset></dl></div>

                      <small id='OUMVD'></small><noframes id='OUMVD'>