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

      <small id='7BHNZ'></small><noframes id='7BHNZ'>

      <tfoot id='7BHNZ'></tfoot>
          <bdo id='7BHNZ'></bdo><ul id='7BHNZ'></ul>
        <legend id='7BHNZ'><style id='7BHNZ'><dir id='7BHNZ'><q id='7BHNZ'></q></dir></style></legend>

      1. 从 python 与 bash 交互

        时间:2023-07-22

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

          • <tfoot id='iJKsY'></tfoot>

                <tbody id='iJKsY'></tbody>

                1. <small id='iJKsY'></small><noframes id='iJKsY'>

                  <legend id='iJKsY'><style id='iJKsY'><dir id='iJKsY'><q id='iJKsY'></q></dir></style></legend>
                  本文介绍了从 python 与 bash 交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在玩 Python 的 subprocess 模块,我想用 python 中的 bash 做一个交互式会话".我希望能够像在终端仿真器上一样从 Python 读取 bash 输出/写入命令.我想一个代码示例可以更好地解释它:

                  I've been playing around with Python's subprocess module and I wanted to do an "interactive session" with bash from python. I want to be able to read bash output/write commands from Python just like I do on a terminal emulator. I guess a code example explains it better:

                  >>> proc = subprocess.Popen(['/bin/bash'])
                  >>> proc.communicate()
                  ('user@machine:~/','')
                  >>> proc.communicate('ls
                  ')
                  ('file1 file2 file3','')
                  

                  (显然,它不是那样工作的.)这样的事情可能吗?如何?

                  (obviously, it doesn't work that way.) Is something like this possible, and how?

                  非常感谢

                  推荐答案

                  试试这个例子:

                  import subprocess
                  
                  proc = subprocess.Popen(['/bin/bash'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
                  stdout = proc.communicate('ls -lash')
                  
                  print stdout
                  

                  您必须阅读有关 stdin、stdout 和 stderr 的更多信息.这看起来不错:http://www.doughellmann.com/PyMOTW/subprocess/

                  You have to read more about stdin, stdout and stderr. This looks like good lecture: http://www.doughellmann.com/PyMOTW/subprocess/

                  另一个例子:

                  >>> process = subprocess.Popen(['/bin/bash'], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
                  >>> process.stdin.write('echo it works!
                  ')
                  >>> process.stdout.readline()
                  'it works!
                  '
                  >>> process.stdin.write('date
                  ')
                  >>> process.stdout.readline()
                  'wto, 13 mar 2012, 17:25:35 CET
                  '
                  >>> 
                  

                  这篇关于从 python 与 bash 交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何从 python 调用程序而不等待它返回 下一篇:子进程似乎在 pyinstaller exe 文件中不起作用

                  相关文章

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

                2. <small id='FhEYB'></small><noframes id='FhEYB'>

                    <tfoot id='FhEYB'></tfoot>

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