使用Python在for循环中设置文本格式
我正在尝试提取系统驱动器的信息。我面临的问题是如何将信息拆分并放到需要的地方。如果有多个卷,则以下为所需输出:使用Python在for循环中设置文本格式,python,text,formatting,Python,Text,Formatting,我正在尝试提取系统驱动器的信息。我面临的问题是如何将信息拆分并放到需要的地方。如果有多个卷,则以下为所需输出: Mountpoint: C:\ OS Volume: True GUID: d1dd2893f42711e09090806e6f6e6963 Mountpoint: D:\ OS Volume: False GUID: b4584ed2e3b211e2ae7d806e6f6e6963 以下是我运行当前所拥有内容时打印出来的信息: Mountpoint: C:\
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)
您的驱动器
变量
正在迭代strdrives
的内容。但是,在循环中,您的应用程序使用独立变量(mount、osvol、guid
)
如果strdrives中有多个条目,则预期结果将是重复的文本块
=>最可能的情况是,您的变量不包含您期望的内容(因此您的拆分没有按预期进行,但是您没有提供它)Bruce,我编辑了我的原始问题以使事情更清楚一些。对于问题的所有意图和目的,“agent”变量都可以忽略