2013-07-15 32 views
0

我試圖爲我的系統驅動器提取信息。我遇到的問題是能夠將信息分解並放在需要去的地方。下面是所需出來放,如果有一個以上的卷:使用Python進行for循環的文本格式化

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 

我知道我做錯了,我不知道如何解決它。我已經瀏覽了互聯網,但無濟於事。這可能只是我的天真。

這裏是我使用的代碼:variable是迭代的strdrives內容

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) 

回答

0

您的驅動器。文字 。然而,你的循環中,您使用獨立的變量(mount, osvol, guid

如果你有一個以上的strdrives一個條目,預期的結果將被複制塊=>最有可能,你的變量不包含你所期望的(因此你的分裂並不像預期的那樣工作,你沒有提供它)然而,

+0

布魯斯,我編輯了我原來的問題,使事情更清楚一點。對於問題的所有意圖和目的,「代理人」變量可以被忽略 – Greg