用Python设置for循环中的文本格式

2024-05-15 07:42:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试获取系统驱动器的信息。我面临的问题是如何将信息拆分并放到需要的地方。如果有多个卷,下面是所需的输出:

Mountpoint: C:\
OS Volume: True
GUID: d1dd2893f42711e09090806e6f6e6963

Mountpoint: D:\
OS Volume: False
GUID: b4584ed2e3b211e2ae7d806e6f6e6963

以下是我运行当前所拥有内容时打印出来的信息:

Mountpoint: C:\
                Mountpoint: D:\

OS Volume: True
                OS Volume: False

GUID: d1dd2893f42711e09090806e6f6e6963
                GUID: b4584ed2e3b211e2ae7d806e6f6e6963

我知道我做错了什么,我不知道该怎么解决。我浏览了这些网络,但没有结果。可能是我太天真了。你知道吗

以下是我使用的代码:

def driveInfo(agent):
    info = "info " + agent + " | grep "
    drives = subprocess.Popen(info + "'\[mountpoints\]'", shell=True, stdout=subprocess.PIPE)
    drives, err = drives.communicate()
    strdrives = str(drives)

    ### Volume Information
    mount = subprocess.Popen(info + "'\[mountpoints\]'", shell=True, stdout=subprocess.PIPE)
    osvol = subprocess.Popen(info + "'\[OSVolume\]'", shell=True, stdout=subprocess.PIPE)
    guid = subprocess.Popen(info + "'\[guid\]'", shell=True, stdout=subprocess.PIPE)

    ### Communicate calls
    mount, err = mount.communicate()
    osvol, err = osvol.communicate()
    guid, err = guid.communicate()

    ### String conversions
    mount = str(mount).strip()
    osvol = str(osvol).strip()
    guid = str(guid).strip()

    ### Drive Information Output
    for drive in strdrives:
            print mount
            print osvol
            print guid

driveInfo(agent)

Tags: infotrueosstdoutshellguidsubprocesspopen