import subprocess import re # info = subprocess.Popen("systeminfo",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE).stdout.read().decode("gbk") # # with open(r"D:/info.txt",'w') as f: # f.write(info) with open(r"D:/info.txt",'r') as f: info = f.read() info2 = re.sub("s","",info) # print(info2) #主机名:PC201808110908 hostname = r"(?P<hostname>主机名Ww{2}d+)" #产品ID:00426-OEM-8992662-00006 cp_id = r"(?P<cp_id>产品IDWd+-w+-d+-d+)" #初始安装日期:2018/8/11,9:15:36 d/dWd:d:d) cz_date = r"(?P<cz_date>初始安装日期Wd+Wd+Wd+Wd+:d+:d+)" #系统类型:x64-based sys_type = r"(?P<sys_type>系统类型Ww+d+-[a-z]+)" #处理器:安装了1个处理器。[01]:Intel64Family6Model58Stepping9GenuineIntel~2601Mhz cpu_type = r"(?P<Intel>Inteld+)(?P<Family>Familyd+)(?P<Model>Modeld+)(?P<Stepping>Steppingd+)(?P<GenuineIntel>GenuineIntelW+d+w{3})" #物理内存总量:8,142MB wl_mer = r"(?P<wl_mer>物理内存总量Wd+Wd+w{2})" #IP地址,MAC地址:IP地址[01]:192.168.1.3[02]:fe80::557c:e951:f509:995b ip_add = r"(?P<ip_add>IP地址[d+]Wd+Wd+Wd+Wd+)(?P<mac>[d+]W(?:w+|SW){9})" data_list = [hostname,cp_id,cz_date,sys_type,cpu_type,wl_mer,ip_add] c = [] for i in data_list: b = re.finditer(i,info2).__next__().groupdict() c.append(b) with open('./PC_MSG.txt','w') as f2: f2.write(str(c))