disruptive
disruptive

Reputation: 5946

Forcing an enter on a program fired off in Python by os.system

I'm using the dirty way of executing a program from python. That program requires an enter from the user before it finishes. How can I force an enter. The issue of course is that I cannot issue any further command until the first command has fully executed so this won't work:

os.system(command)
os.linesep('\r\n')

On of the reasons I went back to using os.system(command) is that I couldn't get command line parameters in in the correct format. One issue is the requirement for command setting="xyz.txt" with the double inverted commas appearing correctly, I've not found a smart way to do this as yet.

Is there a way around this? I'm doing this on Windows 7. I'm concerned that I'll need to examine the output and break when I can see the program has ended. Is there anyway to detected that program is taking keyboard input and use this fact?

Upvotes: 0

Views: 1267

Answers (3)

disruptive
disruptive

Reputation: 5946

This is the answer in reality based on subprocess and works on Windows 7.

p = Popen([batch_file], stdout=PIPE, stdin=PIPE, bufsize=1, shell=True)
while p.poll() is None:
    line = p.stdout.readline()
    if line.startswith('End'):
        p.communicate('\r\n')

Upvotes: 0

Tau
Tau

Reputation: 11

If you want the script at some point to simulate a user's key stroke, then you can follow the solution here: Simulating a key press event in Python 2.7

You will need to download and install pywin32.

Upvotes: 0

MattDMo
MattDMo

Reputation: 102902

Check out the subprocess module, in particular subprocess.Popen(). Once your subprocess is running, you can communicate with it, along with other useful functions.

Upvotes: 7

Related Questions