Subprocess with a Python Session?

  • Thread starter Calvin Spealman
  • Start date
C

Calvin Spealman

No matter what I do I cant get the following code to do what I expect.
I hadn't used subprocess t o read and write to pipes of a
still-running app, and I just can't seem to get it right. What gives?

import subprocess

p = subprocess.Popen("python", stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write('print 10\n')
assert p.stdout.readline() == '10\n'
 
N

Nick Craig-Wood

Calvin Spealman said:
No matter what I do I cant get the following code to do what I expect.
I hadn't used subprocess t o read and write to pipes of a
still-running app, and I just can't seem to get it right. What gives?

import subprocess

p = subprocess.Popen("python", stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write('print 10\n')
assert p.stdout.readline() == '10\n'

To read and write to a still running app, you'll want to use pexpect
probably.

http://pexpect.sourceforge.net/

Note that running python under pexpect puts it into interactive
mode.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top