• <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 交互

        Interacting with bash from python(从 python 与 bash 交互)

        • <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 交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)

                  <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>

                  • <small id='FhEYB'></small><noframes id='FhEYB'>

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

                              <tbody id='FhEYB'></tbody>
                            <legend id='FhEYB'><style id='FhEYB'><dir id='FhEYB'><q id='FhEYB'></q></dir></style></legend>