最近我刚开始使用python3,并意识到python2.6有很多变化。我想知道有没有用fdisk格式化linux系统中可用硬盘的视图?在python2.6中,它是这样工作的
def parse_fdisk(fdisk_output):
result = {}
for line in fdisk_output.split("\n"):
if not line.startswith("/"): continue
parts = line.split()
inf = {}
if parts[1] == "*":
inf['bootable'] = True
del parts[1]
else:
inf['bootable'] = False
inf['start'] = int(parts[1])
inf['end'] = int(parts[2])
inf['blocks'] = int(parts[3].rstrip("+"))
inf['partition_id'] = int(parts[4], 16)
inf['partition_id_string'] = " ".join(parts[5:])
result[parts[0]] = inf
return result
def main():
fdisk_output = commands.getoutput("fdisk -l")
for disk, info in parse_fdisk(fdisk_output).items():
print disk, " ".join(["%s=%r" % i for i in info.items()])
commands
模块已从Python3中删除。您可以改为使用subprocess
模块:未对
parse_fdisk
函数进行任何更改。 唯一需要更改的是在main()
中调用commands.getoutput
。在看看psutil包。在
从他们的README:
磁盘示例:
据我所知,分区细节(例如bootable标志)还不受支持。在
相关问题 更多 >
编程相关推荐