There are questions like this, that show how to interact with a shell in Python, sending stuff to its stdin and reading back stuff from its stdout.
I'd just like to do one more thing on top of that:
- Start a process running a shell -- say:
subprocess.Popen(["powershell --someArgs"])
- interact with that shell/process by:
- sending it the occasional command through stdin (commands that can take a while and spew a lot of output)
- consume each command's output through stdout,
- Identify when each command completes and and get the return code for each command, to know which ones succeed or fail.
I suspect there's no way to do the last part, other than to have the shell itself print its last return code, and have the parent parse that from stdout (also, scanning stdout for the prompt to know when the command finishes?). But I'm asking anyways...
Code samples are welcome but not necessary -- precise descriptions should be good enough :-)
(Ideally, using built-in modules like subprocess
, without needing to install any new packages.)