'Windows CMD life parsing (reading) and filter output with Python
I open CMD in Windows, go to C:\Users\OM\Downloads\DCS-COINS\Munt.G_DCS-COINS\ and there run command DCS-COINS -v
and then I get instantly moving lines with info... some needed, some is not.
This is the example of that lines and they are being added every sec:
Event: mgdc_fa-18c_hornet_st_ifei_temp_r [9]
Event: mgdc_fa-18c_hornet_st_ifei_rpm_texture [0]
Event: mgdc_fa-18c_hornet_st_ifei_temp_texture [0]
Event: mgdc_fa-18c_hornet_st_ifei_ff_texture [0]
Event: mgdc_fa-18c_hornet_st_ifei_noz_texture [0]
Event: mgdc_fa-18c_hornet_st_ifei_oil_texture [0]
Event: mgdc_fa-18c_hornet_st_ifei_bingo_texture [0]
Event: mgdc_fa-18c_hornet_st_ifei_lscale_texture [0]
Event: mgdc_fa-18c_hornet_st_ifei_rscale_texture [0]
Event: mgdc_fa-18c_hornet_st_ifei_l0_texture [0]
Event: mgdc_fa-18c_hornet_st_ifei_r0_texture [0]
Event: mgdc_fa-18c_hornet_st_ifei_l50_texture [0]
Event: mgdc_fa-18c_hornet_st_ifei_r50_texture [0]
Event: mgdc_fa-18c_hornet_st_ifei_l100_texture [0]
Event: mgdc_fa-18c_hornet_st_emerg_instr_int_lt [OFF]
Event: mgdc_fa-18c_hornet_st_ifei_r100_texture [0]
Event: mgdc_fa-18c_hornet_st_ext_wow_left [ON]
Event: mgdc_fa-18c_hornet_st_ifei_lpointer_texture [0]
Event: mgdc_fa-18c_hornet_st_ifei_rpointer_texture [0]
Aircraft: [NO-18C_hornet]: Unavailable in installed DCS-COINS module list!
Event: mgdc_fa-18c_hornet_st_aircraft_connection [OFF]
Removing listeners for previous aircraft
Aircraft: [NONE]: Unavailable in installed DCS-COINS module list!
Event: mgdc_fa-18c_hornet_st_aircraft_connection [OFF]
Removing listeners for previous aircraft
From this, for example, I don't need such info as:
Event: mgdc_fa-18c_hornet_st_ifei
Event: mgdc_fa-18c_hornet_st_ext_wow_left
Event: mgdc_fa-18c_hornet_st_aircraft_connection
I was trying to find out how parse (read) that lines from CMD in realtime and filter with Python, I saw a lot of examples with subprocess, os.system and so on, but all they are about writing to .txt file. So only I've made is such code:
import os
os.chdir('C:\\Users\\OM\\Downloads\\DCS-COINS\\Munt.G_DCS-COINS\\')
os.system('DCS-COINS -v > tmp.txt')
And then I open that file and try to find an info I need, close, reopen and again to use such code to open that file, apply some filter and show lines close to what I need:
import re
f = open('tmp.txt', 'r')
output = f.readlines()
f.close()
stop_lines = [
'Event: mgdc_fa-18c_hornet_st_ifei',
'Event: mgdc_fa-18c_hornet_st_ext_wow_left',
'Event: mgdc_fa-18c_hornet_st_aircraft_connection'
]
query = '|'.join(stop_lines)
for line in output:
if not re.search(query, line):
print(line)
I was trying to find out how to filter os.system output and that show it to me, but it seems like os.system always shows output and the code after it doesn't run till os.system line code works...
So are there any ways to parse (read) line from CMD (or maybe even that CMD command run inside Python script), read every new line, apply filter I've made and only after show me the output? Thanks.
P.S. I've tried subprocess but it says access is dined even if I move that folder from C:\Users.... to D:\
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|