我正在编写一个小脚本,列出了我机器上当前连接的硬盘。我只需要磁盘标识符(disk0),而不是分区ID(disk0s1,disk0s2等) 如何遍历包含diskID和partitionID的数组并删除partitionID条目?这是我到目前为止所做的:
import os
allDrives = os.listdir("/dev/")
parsedDrives = []
def parseAllDrives():
parsedDrives = []
matching = []
for driveName in allDrives:
if 'disk' in driveName:
parsedDrives.append(driveName)
else:
continue
for itemName in parsedDrives:
if len(parsedDrives) != 0:
if 'rdisk' in itemName:
parsedDrives.remove(itemName)
else:
continue
else:
continue
#### this is where the problem starts: #####
# iterate through possible partition identifiers
for i in range(5):
#create a string for the partitionID
systemPostfix = 's' + str(i)
matching.append(filter(lambda x: systemPostfix in x, parsedDrives))
for match in matching:
if match in parsedDrives:
parsedDrives.remove(match)
print("found a mactch and removed it")
print("matched: %s" % matching)
print(parsedDrives)
parseAllDrives()
最后一点是我尝试过的最新事情。绝对愿意走另一条路。
答案 0 :(得分:0)
从
开始尝试allDrives = os.listdir("/dev/")
disks = [drive for drive in allDrives if ('disk' in drive)]
然后,给定磁盘ID只有5个字符长度
short_disks = [disk[:6] for disk in disks]
unique_short_disks = list(set(short_disks))