namedtuple简单用法

from collections import namedtuple
Disk = namedtuple('Disk', 'a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14')


def get_disk_info(device):
    with open('diskstats') as f:
        for line in f:
            line = line.split()
            if line[2] == device:
                return Disk(*(line))
    raise RuntimeError('device {} not found!'.format(device))


def main():
    disk_info = get_disk_info('sda1')
    print(disk_info)
    print('磁盘写次数:{}'.format(disk_info.a8))


if __name__ == '__main__':
    main()

  

猜你喜欢

转载自www.cnblogs.com/superniao/p/10548773.html
0条评论
添加一条新回复