2016-07-21 4 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() 

마지막 비트는 내가 시도한 가장 최근의 것이다. 다른 경로로가는 길은 확실히 열려 있습니다. 다음

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

2 자리 숫자 식별자를 포함하지만 일부 디스크 (예 : disk11)가 있습니다 –

+0

확인하다 .isalpha()'_here [-1]은 마지막 원소를 참조하고, isalpha는 마지막 원소를 가리킨다.()'는 숫자가 아닌 문자인지 검사합니다. – user3036878

관련 문제