1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
import os import re
filename = 'F:/111.txt' resultfilename = 'F:/result.txt' if os.path.exists(resultfilename): os.remove(resultfilename) result_txt = open('F:/result.txt', 'a')
with open(filename, 'r', encoding='utf8') as file_to_read: while True: lines = file_to_read.readline() if "display version" in lines: result_line01 = '=' * 50 + '\n' result_line02 = 'Device Name:\t' + ''.join(re.findall(r'[<](.*?)[>]', lines)) + '\n' result_txt.write(result_line01) print(result_line01) print(result_line02) result_txt.write(result_line02) if "Comware Software" in lines: result_line03 = 'Comware Software:\t' +''.join(re.findall('(?<=Software, ).*$', lines)) + '\n' print(result_line03) result_txt.write(result_line03) if "uptime is" in lines: result_line04 = 'Uptime:\t' + ''.join(re.findall('(?<=uptime is ).*$', lines)) + '\n' print(result_line04) result_txt.write(result_line04) if "DEVICE_SERIAL_NUMBER" in lines: result_line05 = 'SN:\t' + ''.join(re.findall('(?<=:).*$', lines)) + '\n' result_line05 = result_line05.replace(' ', '') print(result_line05) result_txt.write(result_line05) if "Slot 1 CPU 0 CPU usage:" in lines: result_line06 = next(file_to_read) result_line06 = result_line06[5:9] + '\n' result_line06 = 'cpu-usage:\t' + result_line06.replace(' ', '') print(result_line06) result_txt.write(result_line06) if "Slot 1 CPU usage:" in lines: result_line066 = next(file_to_read) result_line066 = result_line066[5:9] + '\n' result_line066 = 'cpu-usage:\t' + result_line066.replace(' ', '') print(result_line066) result_txt.write(result_line066) if "Unit CPU usage:" in lines: result_line0666 = next(file_to_read) result_line0666 = result_line0666[5:9] + '\n' result_line0666 = 'cpu-usage:\t' + result_line0666.replace(' ', '') print(result_line0666) result_txt.write(result_line0666) if "Used Rate" in lines: result_line07 = lines[-4:] result_line07 = 'memory-usage:\t' + result_line07.replace(' ', '') print(result_line07) result_txt.write(result_line07) if "Mem:" in lines: result_line08 = lines[-6:] result_line08 = result_line08.replace(' ', '') result_line08 = result_line08.replace('%', '') result_line08 = 100 - float(result_line08) result_line08 = '%.1f%%' % result_line08 result_line08 = 'memory-usage:\t' + result_line08 + '\n' print(result_line08) result_txt.write(result_line08) result_txt.flush() if "结束记录时间" in lines: break
|