There is some kind of application in which there is a console (opens by pressing a button), where user actions are written. For example, he chose a menu item - immediately it is reported in the console as a separate line. And there is an application written in Python that needs to be taught to read records from this console in real time. I am not so long ago in programming, so I ask for your help - my searches in Google did not give any special results, since I don’t even know the principle of operation and the name of the whole process. We need some articles / books / etc., Where it is told in accessible language about such things in the Python language, in English. Thank you in advance.
1 answer
from subprocess import PIPE, Popen with Popen('cmd', shell=True, stdout=PIPE, stdin=PIPE) as bat: bat.stdin.write('ping 127.0.0.1\n'.encode()) bat.stdin.close() for o in bat.stdout: print(o.decode('utf-8', 'ignore')) print('my_string')
Out:
C:\Python344\python.exe E:/python/2016/2/2222.py Microsoft Windows [Version 6.3.9600] my_string (c) ௮ (Microsoft Corporation), 2013. ࠢ 饭. my_string my_string E:\python\2016\2>ping 127.0.0.1 my_string my_string ⠬ 127.0.0.1 32 ⠬ : my_string ⢥ 127.0.0.1: =32 ६<1 TTL=128 my_string ⢥ 127.0.0.1: =32 ६<1 TTL=128 ... E:\python\2016\2> my_string
Apparently you need something like this and read about the subprocess
- oneThe author seems to be a GUI program that prints into its own window, and not the console (no standard streams). Related: Interact with other programs using Python . If the console program that uses stdin, stdout, stderr, then right in the direction of the
subprocess
,pexpect
modules should be looked at. - jfs - @jfs That's right, a GUI program. It seems to be more or less clear, thanks for the link, but there was a question (perhaps stupid): if the program is written using pywin32, will the reading from the same application work, if the program and the application are run on another OS, is there not on windows? - Newbie
- @NickCaulfield answer: no, yes, it depends. You'd better update the question or ask a new one where it describes in detail what you want to get, and only then describe unsuccessful solution attempts (pywin32). If you need a portable program, then trying to scrape the text, drawn on the screen by some unspecified GUI program, is not the best option (the best option depends on what program it is: can you find libraries, console options by analogy with this answer ) - jfs
|