使用python3.x查看Linux中的分区

使用python3.x查看Linux中的分区,python,python-3.x,Python,Python 3.x,最近我刚开始使用python3,并意识到python2.6做了很多更改。我想知道在linux系统中是否有使用fdisk格式化硬盘视图的方法?在python2.6中,它是这样工作的 def parse_fdisk(fdisk_output): result = {} for line in fdisk_output.split("\n"): if not line.startswith("/"): continue parts = line.spli

最近我刚开始使用python3,并意识到python2.6做了很多更改。我想知道在linux系统中是否有使用fdisk格式化硬盘视图的方法?在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是一个模块,它提供了一个接口,通过使用Python以可移植的方式检索所有正在运行的进程和系统利用率(CPU、磁盘、内存)的信息,实现了命令行工具提供的许多功能,如:ps、top、df、kill、free、lsof、netstat、ifconfig、nice、ionice、iostat、iotop、uptime、tty

从他们的:

它目前同时支持LinuxWindowsOSXFreeBSD 32位64位,通过使用 单个代码库

磁盘示例:

>>> 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)

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

Python3中的
命令模块已被删除。您可以改用
子流程
模块:

import subprocess
import shlex
import sys

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():
    proc = subprocess.Popen(shlex.split("fdisk -l"),
                            stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    fdisk_output, fdisk_error = proc.communicate()
    fdisk_output = fdisk_output.decode(sys.stdout.encoding)
    for disk, info in parse_fdisk(fdisk_output).items():
        print(disk, " ".join(["%s=%r" % i for i in info.items()]))

main()
未对
parse_fdisk
函数进行任何更改。
唯一需要更改的是在
main()

中调用
命令.getoutput
,这是一个非常好的模块。然而,是否有任何我可以抓取分区的细节?除了硬盘名称之外,我还需要查看硬盘的分区id。对您的问题的实际描述会很有用。