Making python run the file on the command line, type something, wait, and then type something else

In python, I wanted to do the following: I have a command line program that requires the user to input input step by step and wait between them to get the result. Now I want to automate this process with python.

The process will look something like this:

  • run myProgram.exe at the command prompt
  • enter command 1
  • wait for command 1 to complete and complete (takes ~ 5 minutes)
  • enter command 2 ...

Is there any way to simulate this process in python? I knew that we could run the program and pass command line parameters using os.open () or a subprocess. But this is a one-time thing.

thanks

+4
source share
2 answers

You can use the subprocess module and Popen.communicate() to send data to the stdin process

EDIT:

You're right, it should use stdin.write to send data, something like this:

 #!/usr/bin/env python import subprocess import time print 'Launching new process...' p = subprocess.Popen(['python', 'so1.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) print 'Sending "something"...' p.stdin.write('something\n') print 'Waiting 30s...' time.sleep(30) print 'Sending "done"...' p.stdin.write('done\n') p.communicate() print '=o=' 
+2
source

On Unix or with cygwin python on Windows, the pexpect module can automate interactive programs.

See examples here . You want to pass the timeout argument longer than the default for expect() for the part that takes five minutes.

+2
source

Source: https://habr.com/ru/post/1381392/


All Articles