2013-03-12 6 views
0

질문이 반복 될 수 있습니다. 그러나 여기 질문 설명합니다 코드입니다 :이 프로그램에서파이썬을 사용하여 외부 파일의 데이터를 목록에 저장

#compares partitions displayed by 'df' command with the required list of partitions. 

import subprocess 
import re 


p = subprocess.Popen("df -h", stdout=subprocess.PIPE, shell=True) 
dfdata, _ = p.communicate() 

dfdata = dfdata.replace("Mounted on", "") 

columns = [list() for i in range(10)] 
for line in dfdata.split("\n"): 
    line = re.sub(" +", " ", line) 
    for i,l in enumerate(line.split(" ")): 
     columns[i].append(l) 


sys_partition = columns[5] 
req_partition = ['/', '/boot', '/home', '/usr', '/usr/local', '/var', '/tmp', '/mnt/floppy', '/mnt/cdrom', '/dev/shm'] 


def remove_common_elements(sys_partition, req_partition): 
    sys_partition_new = sys_partition[:] 
    req_partition_new = req_partition[:] 

    for i in sys_partition: 
     if i in req_partition_new: 
      sys_partition_new.remove(i) 
      req_partition_new.remove(i) 
return req_partition_new 

if set(sys_partition) == set(req_partition): 
    print 'All the partitions exist in your system!!!' 
else: 
    print 'These partitions do not exist: ', 
    print (remove_common_elements(sys_partition, req_partition)) 

는 'sys_partition는'(df 명령으로 표시) 시스템 INT 현재 파티션과 'req_partition'를 의미 적으로해야 파티션 목록을 의미 선물. 이제 필요한 파티션 (req_partition)을 하드 코딩하고 싶지 않습니다. 일부 외부 파일에서 그 입력을 받아서 목록에 저장 한 다음 표시된 것처럼 처리하려고합니다. 도와주세요. 질문이 혼란 스럽다면 더 자세히 설명하려고 노력할 것입니다. 미리 감사드립니다.

답변

0

파일을 읽고 어떻게 항목을 목록에 넣는 지 묻는 것처럼 보입니다.

req_partition = ['/', '/boot', '/home', '/usr', '/usr/local', '/var', '/tmp', '/mnt/floppy', '/mnt/cdrom', '/dev/shm'] 

이 라인을 교체 : 그렇다면,이 라인 제거

/ 
/boot 
/home 
/usr 
/usr/local 
/var 
/tmp 
/mnt/floppy 
/mnt/cdrom 
/dev/shm 
: /path/to/partitions

req_partition = list() 
with open('/path/to/partitions') as f: 
    for line in f: 
     req_partition.append(line.strip()) 

을하고 파일을 확인을 이러한 내용을 가지고

관련 문제