Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

communication with a shell program programatically

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
martoro
Posts: 2
Joined: 2006-07-17 23:29

communication with a shell program programatically

#1 Post by martoro »

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!

ajdlinux
Posts: 2452
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

#2 Post by ajdlinux »

Use os.popen, e.g.

Code: Select all

shellscript = os.popen("shellscript.sh")
shellscript.write("Data")
shellscript.write("Blah blah blah")
shellscript.close()

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

#3 Post by martoro »

Thanks ajdlinux,

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

Thanks

ajdlinux
Posts: 2452
Joined: 2006-04-23 09:37
Location: Port Macquarie, NSW, Australia

#4 Post by ajdlinux »

Like you do with any other file:

Code: Select all

x = shellscript.read()
# do something with x here

Post Reply