1

I currently have this code, whcih works and produces the output you would expect, ie a list of the output lines if you were to run 'ls -ltr | less' from the terminal.

p1 = subprocess.Popen(shlex.split('ls -ltr'), stdout=subprocess.PIPE,
                                                        stderr=subprocess.PIPE)
p2 = subprocess.Popen(shlex.split('less'), stdin=p1.stdout,
                               stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print p2.communicate()

Is there a way using subprocess.Popen or anything else to get the interactive output you can scroll through and enter keyboard commands into as you would if you ran the commands directly from the terminal?

1
  • I think that people usually suggest something like pexpect for this sort of thing (although I've never used it myself). Commented Jul 30, 2012 at 12:20

1 Answer 1

2

If the Python process has its own stdout connected to a terminal, then that's

p1 = subprocess.Popen(shlex.split('ls -ltr'), stdout=subprocess.PIPE,
                                              stderr=subprocess.PIPE)
p2 = subprocess.Popen(shlex.split('less'), stdin=p1.stdout)

i.e. don't redirect the output from less.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.