communication with a shell program programatically

Need help with C, C++, perl, python, etc?

communication with a shell program programatically

Postby martoro » 2006-07-17 23:35

Hello,

I'm trying to figure out how to communicate with a shell program which expects some input from stdin from within a program, preferably a python program. To further explain, I'd like to run the program using

Code: Select all
os.system(cmd)


in my python script and then instead of feeding the input to the cmd program from stdin, I'd like to be able to feed the input from within my script.

Thanks in advance!
martoro
 
Posts: 2
Joined: 2006-07-17 23:29

Postby ajdlinux » 2006-07-17 23:42

Use os.popen, e.g.

Code: Select all
shellscript = os.popen("shellscript.sh")
shellscript.write("Data")
shellscript.write("Blah blah blah")
shellscript.close()
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

Postby martoro » 2006-07-18 01:01

Thanks ajdlinux,

How can i read data from the shellscript object? I couldn't find some decent explanation of os.popen.

Thanks
martoro
 
Posts: 2
Joined: 2006-07-17 23:29

Postby ajdlinux » 2006-07-18 05:07

Like you do with any other file:

Code: Select all
x = shellscript.read()
# do something with x here
ajdlinux
 
Posts: 2480
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia


Return to Programming

Who is online

Users browsing this forum: No registered users and 8 guests

fashionable