Interacting with another command line program in Python

后端 未结 2 1721
轮回少年
轮回少年 2021-02-02 02:55

I need to write a Python script that can run another command line program and interact with it\'s stdin and stdout streams. Essentially, the Python script will read from the tar

2条回答
  •  清酒与你
    2021-02-02 03:27

    To perform such detailed interaction (when, outside of your control, the other program may be buffering its output unless it thinks it's talking to a terminal) needs something like pexpect -- which in turns requires pty, a Python standard library module that (on operating systems that allow it, such as Linux and Mac OS x) implements "pseudo-terminals".

    Life is harder on Windows, but maybe this zipfile can help -- it's supposed to be a port of pexpect to Windows (sorry, I have no Windows machine to check it on). The project in question, called wexpect, lives here.

提交回复
热议问题