#! /usr/bin/env python # -*- coding: utf-8 -*- from xml.etree.ElementTree import Element, SubElement, Comment, tostring import os f = str(os.system("sudo fdisk -l")) #f = open('file.txt') top = Element('Disks') def crDiskElement(info, top): child = SubElement(top, 'Disk') childDiskPath = SubElement(child,'Path') childDiskPath.text = info[1][:-1] childDiskSize = SubElement(child,'Size') childDiskSize.text = info[2]+" "+info[3][:-1] childDiskSectors = SubElement(child,'Sectors') childDiskSectors.text = info[6] return top for line in f: l = line.split(" ") for element in l: if element.decode('utf-8') == 'Диск'.decode('utf-8'): top = crDiskElement(l,top) else: pass text_file = open("Output.xml", "w") text_file.write(tostring(top)) text_file.close() #print tostring(top)