1import encodings 2import os 3import re 4from xml.dom.minidom import parse 5 6def get_devices_sn(): 7 cmd_sn = os.popen("hdc_std list targets").read() 8 device_sn = re.findall('[\w+]{32}', cmd_sn) + re.findall('[\w+]{16}', cmd_sn) 9 dom_tree = parse('config\\user_config.xml') 10 collection = dom_tree.documentElement 11 sn1 = collection.getElementsByTagName('sn')[0] 12 if len(device_sn[0]) == len(device_sn[1]): 13 sn1.childNodes[0].data = "{};{}".format(device_sn[0], device_sn[1]) 14 else: 15 sn1.childNodes[0].data = device_sn[0] 16 with open('config\\user_config.xml', 'w', encoding='utf-8') as f: 17 dom_tree.writexml(f, encoding='utf-8') 18 f.close() 19 20if __name__ == '__main__': 21 get_devices_sn()