2016-07-21 13 views
0

私のマシン上に現在接続されているハードディスクをリストする小さなスクリプトを書いています。パーティションID(disk0s1、disk0s2など)ではなく、ディスク識別子(disk0)のみが必要です diskIDとpartitionIDを含む配列を反復処理し、partitionIDエントリを削除するにはどうすればよいですか?これまでに私が試みてきたことは次のとおりです。文字列配列から反復文字列を削除する

import os 

    allDrives = os.listdir("/dev/") 
    parsedDrives = [] 

    def parseAllDrives(): 
     parsedDrives = [] 
     matching = [] 
     for driveName in allDrives: 
      if 'disk' in driveName: 
       parsedDrives.append(driveName) 
      else: 
       continue 
     for itemName in parsedDrives: 
      if len(parsedDrives) != 0: 
       if 'rdisk' in itemName: 
        parsedDrives.remove(itemName) 
       else: 
        continue 
      else: 
       continue 

#### this is where the problem starts: ##### 

     # iterate through possible partition identifiers 
     for i in range(5): 
      #create a string for the partitionID 
      systemPostfix = 's' + str(i) 
      matching.append(filter(lambda x: systemPostfix in x, parsedDrives)) 

     for match in matching: 
      if match in parsedDrives: 
       parsedDrives.remove(match) 
       print("found a mactch and removed it") 

     print("matched: %s" % matching) 
     print(parsedDrives) 

    parseAllDrives() 

最後のビットは私が試した最新のものです。間違いなく別のルートに行くことをお勧めします。ディスクIDが与えられ、その後

allDrives = os.listdir("/dev/") 
disks = [drive for drive in allDrives if ('disk' in drive)] 

で始まる

答えて

0

試みは、唯一の5文字の長さ

short_disks = [disk[:6] for disk in disks] 
unique_short_disks = list(set(short_disks)) 
+0

ているいくつかのディスクがあります。しかし、その後、[OK]を –

+0

(すなわちdisk11)2桁の識別子が含まれています最初に '[disk [:7] ...]でフィルタリングし、' not unique_short_disk [-1] .isalpha() 'で最後の要素をチェックすることができます_here [-1]は最後の要素を参照し、' isalpha () 'は数字ではなく文字かどうかをチェックします。 – user3036878

関連する問題