How to get inputs for a python program that run from another python program

P

pradeep nair

I would like to know how to pass keyboard input for a python script
which is ran by another script.

for eg:

hello1.py:

import os

if __name__=='__main__':

print "I will call this other program called hello.py"
os.system("python hello.py")
print "hello1.py"


hello.py:

import os

if __name__=='__main__':

print "press ENTER to display"
#code wer if the user hits enter
print "hello"
#else the user hits any other keyboard button:
sys.exit()


now wen i run hello1.py,i want the some function or utility in
hello1.py that can pass the keyboard i/p to hello.py .
 
P

pyscottishguy

I would like to know how to pass keyboard input for a python script
which is ran by another script.

for eg:

hello1.py:

import os

if __name__=='__main__':

print "I will call this other program called hello.py"
os.system("python hello.py")
print "hello1.py"

hello.py:

import os

if __name__=='__main__':

print "press ENTER to display"
#code wer if the user hits enter
print "hello"
#else the user hits any other keyboard button:
sys.exit()

now wen i run hello1.py,i want the some function or utility in
hello1.py that can pass the keyboard i/p to hello.py .

Using pexpect: http://pexpect.sourceforge.net/

hello1.py:

import pexpect

if __name__=='__main__':

print "I will call this other program called hello.py"

child = pexpect.spawn('python hello.py')
child.expect ('\n')

print "Received from hello.py: ", child.before

entered = raw_input("> ")
child.sendline (entered)
child.expect ('\n')

print "Received from hello.py: ", child.before
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top