gpt4 book ai didi

python - 使用 python3.x 在 Linux 中查看分区

转载 作者:行者123 更新时间:2023-11-28 20:51:43 27 4
gpt4 key购买 nike

最近我刚开始使用python3,发现python2.6 有很多变化。我想知道是否有通过使用 fdisk 格式化 linux 系统中可用硬盘的 View ?在 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()])

最佳答案

看看 psutil包裹。

psutil is a module providing an interface for retrieving information on all running processes and system utilization (CPU, disk, memory) in a portable way by using Python, implementing many functionalities offered by command line tools such as: ps, top, df, kill, free, lsof, netstat, ifconfig, nice, ionice, iostat, iotop, uptime, tty.

来自他们的 README :

It currently supports Linux, Windows, OSX and FreeBSD both 32-bit and 64-bit with Python versions from 2.4 to 3.3 by using a single code base.

磁盘示例:

>>> psutil.disk_partitions()
[partition(device='/dev/sda1', mountpoint='/', fstype='ext4'),
partition(device='/dev/sda2', mountpoint='/home', fstype='ext4')]
>>>
>>> psutil.disk_usage('/')
usage(total=21378641920, used=4809781248, free=15482871808, percent=22.5)
>>>
>>> psutil.disk_io_counters()
iostat(read_count=719566, write_count=1082197, read_bytes=18626220032,
write_bytes=24081764352, read_time=5023392, write_time=63199568)

据我所知,(目前)还不支持分区详细信息(例如可引导标志)。

关于python - 使用 python3.x 在 Linux 中查看分区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8869574/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com