简体   繁体   中英

How to get the Input and Output of an external Executable?

I wannted to dig deeper in automation but didnt find anything about gettin information from any Executable.

What i want is to start an exe via Python so python can watch what happens when i press a button or do smt else in that external Programm. And with that given data i want to automate the external Programm and give it the Data to press the button it self.

Like silenium, where i know the code and can directly interact with it.

Is there any way to do so?

I was looking for that type of automation. And landed somewhere where u can use subprocess but couldnt find a way to give me that what i want to do or i just didnt get it right.

You can use the subprocess module in Python to start an external executable and interact with it. To get the output of the executable, you can use the PIPE argument with the subprocess.run method and retrieve the output using the stdout attribute. For example:

import subprocess

result = subprocess.run(['executable.exe'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

output = result.stdout.decode()
print(output)

To send input to the executable, you can use the stdin argument with the subprocess.Popen method and write to the stdin attribute of the Popen object. For example:

import subprocess

process = subprocess.Popen(['executable.exe'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.stdin.write(b'input data\n')
process.stdin.flush()
output = process.stdout.readline()
print(output)

Keep in mind that the interaction with an external executable depends on how the executable was designed and may not be possible for all executables.

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