
November 26th, 2012, 08:03 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 6
Time spent in forums: 2 h 16 m 38 sec
Reputation Power: 0
|
|
|
Subprocess - then read - then write to same shell?
Please can someone explain how to do this:
I want to make some commands in a shell - read the output and based on the output make another input in the same shell
Code:
import subprocess
p = subprocess.Popen('some_program', stdin = subprocess.PIPE, stdout = subprocess.PIPE)
out, err = p.communicate()
if "abcdefg" in out:
p.communicate(input = ' -c -d 1234')
out, err = p.communicate()
if "xyz" in out:
print "I have been able to open a shell, read the output and give another command to the same shell"
I have looked at readlines, stdin.write etc but I am lost as to what the solution will be. It seems that as soon as I 'read' the output the process is then closed preventing me from giving any further inputs. If I open a new shell then its no good - because the 2nd commands must come after the 1st (if it meets a condition).
|