Search code examples
pythonpython-interactive

In Python, how could I shell out and interact with an external program (say vi or nano) and return to python


My first question on stackoverflow!

I'm trying to figure out how I can start an external program that requires the user to interact with it, say vi, nano, ssh, telnet etc. and return to the Python script when the program exits.

I don't want to use send/expect or automate the external program at all, just start it, use it as normal and then return to the script. I guess bash seems like a more natural way to do this, but I was hoping to get it done in Python.


Solution

  • use the integrated subprocess module. It works quite well with interactive applications. subprocess.call starts an application and is blocking until the application exits.

    e.g. starting vi and after exit it starts a connection to 127.0.0.1 over ssh:

    import subprocess
    subprocess.call(["vi"])
    subprocess.call(["ssh","127.0.0.1"])