比较来自行列表的列表



使用parted -l输出

Model: ATA KINGSTON SH103S3 (scsi)
Disk /dev/sda: 120GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number  Start   End     Size    Type      File system  Flags
1      1049kB  1024MB  1023MB  primary   ext4         boot
2      1026MB  120GB   119GB   extended
5      1026MB  120GB   119GB   logical                lvm

Model: ATA WDC WD10EZEX-00W (scsi)
Disk /dev/sdb: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number  Start   End     Size    File system  Name     Flags
1      1049kB  1000GB  1000GB  ext4         primary

Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/system-root: 103GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags:
Number  Start  End    Size   File system  Flags
1      0.00B  103GB  103GB  ext4

Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/system-swap: 16.4GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags:
Number  Start  End     Size    File system     Flags
1      0.00B  16.4GB  16.4GB  linux-swap(v1)

使用parted -lm输出

BYT;
/dev/sda:120GB:scsi:512:512:msdos:ATA KINGSTON SH103S3:;
1:1049kB:1024MB:1023MB:ext4::boot;
2:1026MB:120GB:119GB:::;
5:1026MB:120GB:119GB:::lvm;
BYT;
/dev/sdb:1000GB:scsi:512:4096:gpt:ATA WDC WD10EZEX-00W:;
1:1049kB:1000GB:1000GB:ext4:primary:;
BYT;
/dev/mapper/system-root:103GB:dm:512:512:loop:Linux device-mapper (linear):;
1:0.00B:103GB:103GB:ext4::;
BYT;
/dev/mapper/system-swap:16.4GB:dm:512:512:loop:Linux device-mapper (linear):;
1:0.00B:16.4GB:16.4GB:linux-swap(v1)::;

我有这样的文本,我要做的是

在以"Model:"开头的每一行为磁盘创建循环

然后在for循环3次,得到3项,如果行以Model开头:磁盘[我]。模型=直线以磁盘开头的行磁盘[我]。Size = line以&quot开头的行1";磁盘[我]。Partition = line

我可以找到哪个分区有关键字boot

For disk in disk[i]

如果磁盘[我]。分区包含"引导";DiskContainOs = disk[i].model.

像这样使用双for循环是正确的吗?

def getMainDisk():
#get disk info using "parted -lm" sample like from outputParted
#for loop to find "BYT;"
#if found then get next 2 line
disk = []
for i, line in enumerate(outputParted.splitlines()):
if line.startswith("BYT;"):
disk.append([])

#for j loop next 2 line after found BYT;
for j in outputParted.splitlines()[i+0:i+1]:
disk[i].append(j)

#compare disk if have "boot" keyword then append disk[x] to Maindisk
# print (Maindisk)

get error:

line 265, in getMainDisk
disk[i].append(j)
IndexError: list index out of range
root@pc:~# 

任何想法?

我回答自己,不使用嵌套的for循环

因为parted的输出是恒定的,一开始会显示2个物理驱动器,而且我的系统也一直只有2个驱动器,更重要的是我只想知道哪个磁盘包含操作系统,哪个磁盘有关键字boot。所以我在for循环中使用if else

谢谢所有。

def getMainDisk():
outputParted = subprocess.check_output(['parted', '-lm'])
outputParted = outputParted.decode('utf-8')
# print(outputParted)
# print(outputParted)
disk = []
for i, line in enumerate(outputParted.splitlines()):
if 'BYT' in line:
# print(line)
print(i)
# print(outputParted.splitlines()[i+1])
# print(outputParted.splitlines()[i+1].split(':')[0])
diskName = outputParted.splitlines()[i+1].split(':')[6]
diskSize = outputParted.splitlines()[i+1].split(':')[1]
lineContainboot = outputParted.splitlines()[i+2]
# print(lineContainboot)
if 'boot' in lineContainboot:
# print(lineContainboot)
# print(lineContainboot.split(':')[0])
disk.append(diskName)
disk.append(diskSize)
print(disk)
print('xx')
r = disk[0] + ' ' + disk[1]
return r
else:
pass
else:
pass

相关内容

  • 没有找到相关文章

最新更新