views:

456

answers:

2

Hi,

I am writing a script to automate running a particular model. When the model fails, it waits for a user input (Enter key). I can detect when the model has failed, but I am not able to use python (on linux) to simulate a key press event. Windows has the SendKeys library to do this but I was wondering if there is a similar library for python on linux.

Thanks!

+1  A: 

http://people.csail.mit.edu/adonovan/hacks/xsendkey.html

baol
You can obviously take a look at the rather simple code of xsendkey and search for the corresponding calls in the Xlib binding for python.
baol
A: 

If the "model" is running graphically (with the X window system), the already-suggested xsendkey is a possibility, or xsendkeycode. If it's running textually (in a terminal window), then pexpect.

Alex Martelli
Thanks! pecpect should do the trick for me.
Hi!A corollary:While using pexpect, the following simple program doesn't seem to work for me:import pexpectchild = pexpect.spawn('ls')fout = file('output.txt', 'w')child.logfile = foutI.e, output.txt is created but is empty when I cat it.pexpect is installed fine, since I can run the samples supplied with the program.Any suggestions would be appreciated!thanks!