There is some application, let's say ConsoleApplication.exe. When it starts, it offers to enter some value, and in response it displays something else.

It is necessary for the program to do all this in a loop, that is, for the program to launch the application itself, insert some phrase, and write the result to some file.

I tried to run the application through os.system , but it is not clear how to substitute an argument and simulate a carriage return.

An example of what happens in the console when the application starts:

Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΊΠ»ΡŽΡ‡: //здСсь_Π²Π²ΠΎΠΆΡƒ_ΠΊΠ»ΡŽΡ‡ ΠΊΠ°ΠΊΠΎΠΉ_Ρ‚ΠΎ_ΠΎΡ‚Π²Π΅Ρ‚ 

How can this algorithm be implemented?

    1 answer 1

    For python there is a special module - subprocess :

     from subprocess import Popen, PIPE, STDOUT p = Popen('ConsoleApplication.exe', stdout=PIPE, stdin=PIPE, stderr=STDOUT) p_stdout, p_stderr = p.communicate(input=b'112233') print(p_stdout.decode()) # Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ ΠΈΠ· консоли ConsoleApplication.exe 

    Another example:

    Wrote a google_search.py ​​file:

     #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'ipetrash' key = input('Search: ') print('Search "{}"'.format(key)) import webbrowser webbrowser.open_new_tab('https://www.google.ru/search?q=' + key) 

    And I call, that script is called, it processes the input data (stdin), displays it (stdout) and opens the default browser tab:

     from subprocess import Popen, PIPE, STDOUT p = Popen(['python', 'google_search.py'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) p_stdout, p_stderr = p.communicate(input='ΠšΠΎΡ‚ΠΈΠΊΠΈ Π² Ρ‡Π°ΡˆΠΊΠ΅'.encode()) print(p_stdout.decode()) # Search: Search "ΠšΠΎΡ‚ΠΈΠΊΠΈ Π² Ρ‡Π°ΡˆΠΊΠ΅" 
    • And what does it have to do with it? To me, the program displays "Enter key", and the python task enter this key. - Igor
    • I misunderstand you, well then you need to correct the answer - gil9red
    • This is a third-party process. Can a key be sent using a signal? - Igor