简体   繁体   中英

Simple Windows example of automating a .exe program using subprocess and .Popen

I am new to the subprocess module, and after reading the Python documents among many other websites (including Stack Overflow), I am struggling to find simplified examples of .Popen , .communicate , and other such useful classes. Often, the examples never use each class in succession, only on their own. Also, many examples are Linux-based, such as the use of ["ls", "-l"] , which makes it quite difficult to understand for Windows users.

After several hours playing with this module, I have encountered several issues, which could be best illustrated through the method of opening and communicating with a simple .exe command line program.

For example, say that the program is called "numbers.exe", and that it asks the following questions:

>>> Question 1) Choose 1, 2 or 3
>>> Question 2) Choose 4, 5 or 6
>>> You have answered #(Q1) and #(Q2)
>>> *Use these values in an iterative sequence displaying each stage in the iteration*

I then want to manipulate this program automatically, ie I want python to input 2 and 6 without me having to do anything, but still print the questions. I then want to be able to view the iteration in python.

The first consideration here is that I could use:

from subprocess import Popen, PIPE

numprog = subprocess.call('numbers.exe')
print(numprog.communicate())

However, this just opens the program, and I would still have to enter 2 and 6 myself. To automate the process, I believe I must use Popen , alongside stdin, stdout and stderr. This is where I encounter issues. I understand I must use Popen to begin communicating with the input (stdin), output (stdout) and error (stderr) pipes:

from subprocess import Popen, PIPE

numcomms = Popen('numbers.exe', stdout=PIPE, stdin=PIPE, stderr=PIPE)

I am unsure what to do from here. Using numcomms.stdout.read() causes the program to just linger, and using numcomms.stdin.write(2) throws out an error that int values cannot be used. The numprog.communicate class seems to require you to enter the values yourself.

From what I can see, the pseudo code would be along the lines:

>>> Open numbers.exe stdin, stdout and stderr pipes using Popen
>>> Print first question using stdout
>>> Enter "2" using stdin
>>> Print second question using stdout
>>> Enter "6" using stdin
>>> Receive a string saying "You have answered 2 and 6" using stdout
>>> Display the results of each stage of the iteration using stdout

How would I go about writing this?

Really appreciate the help, thank you!

Edit : Edited the question to describe the iterative sequence issue. Michael has suggested a good resolution to the inputting issue, however I am having trouble printing the results of the iteration.

The problem with subprocess.stdin.write is probably that you need to provide a string instead of an integer, as Steve Barnes pointed out correcly.

However, for your simple case their might be an easier solution. The communicate method has an optional parameter for input, so this should work:

from subprocess import Popen, PIPE

numcomms = Popen('numbers.exe', stdout=PIPE, stdin=PIPE, stderr=PIPE)
out, err = numcomms.communicate("2\n6\n")

The output of the program should be in out afterwards, and can easily be splitted using out.splitlines() .

我认为您对subprocess.stdin.write的问题是程序将期望字符串可能以换行符终止,即“ 2 \\ n”而不是int。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM