0

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?

disruptive
  • 5,687
  • 15
  • 71
  • 135
  • 1
    Can you give a bit more context? What operating system? What command are you executing? – L3viathan Feb 03 '15 at 15:04
  • Its a custom program that needs to be run-inside a massive loop thousands and thousands of times. Its Windows 7. – disruptive Feb 03 '15 at 15:08
  • check `subprocess` module and `communicate` method. – Łukasz Rogalski Feb 03 '15 at 15:13
  • `Popen(['echo', 'command setting="xyz.txt"'])` I fail to see the problem, and it seems unrelated to the first question. If it is so windows-dependent and it really doesn't work, start a new question and be more explicit (and use the appropriate tags). – MariusSiuram Feb 04 '15 at 09:59

3 Answers3

7

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

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Yes, this is what I found. I'm looking now at doing it this way! – disruptive Feb 03 '15 at 15:21
  • 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. – disruptive Feb 03 '15 at 15:48
  • @Navonod it's generally not considered good form to continuously edit your question with new problems that arise, as we are not notified of those changes, and the answers given earlier now look like they don't apply. Please revert to your original question, then ask a new one with the new details. Please include the actual code you are using, the output you are getting, the output you want to get, and the full text of any errors or tracebacks you are receiving. – MattDMo Feb 03 '15 at 16:18
  • You also want `stdin=subprocess.PIPE` when creating an object and then `obj.stdin.write("\n")` or similar to send newline – Dima Tisnek Feb 04 '15 at 14:40
0

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.

Community
  • 1
  • 1
Tau
  • 11
  • 2
0

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')
disruptive
  • 5,687
  • 15
  • 71
  • 135