2013-04-17 2 views
0

좋아, 내가 다음과 같이 여러 목록 상자에서 출력 한 후, 배열에 저장, ONLY ONE .log 파일의 내용을 복용하고있는 중이 야 :배열에 여러 파일에서 데이터의 목록을 저장

filelog_ext=".log" #log extension 

#open one log file 
f = open("%s\%s%s" % (path,filename,filelog_ext),"r") 
#init aray  
array = [] 
#copy contents of file to array 
for line in f: 
    array.append(line) 
f.close() 
#insert each array content to multilistbox 
self.mlb.insert (END, ('%s' % (array[0]),'%s' % (array[1]),'%s' % (array[2]),'%s' % array[3]))) 
    self.mlb.pack (expand=YES,fill=BOTH) 

내가 지금하고 싶은 것은 많은 .log 파일의 내용을 가져 와서 배열로 출력 한 다음 다중 목록 상자에서 출력하는 것입니다.

나는 시도 다음 그러나 작동하지 않습니다

for files in os.listdir("."):   
     if files.endswith(".log"): #this will take all my files ending with .log 

    #not sure how to go about and take all files ending with .log, copying them to array and saving them to the multilistbox mlb 

사람이 좀 도와 주시겠습니까?

업데이트 : @john zwink 나는 당신의 코드를 통합하려고 시도했는데, 그것이 올바른 방법으로하고 있는지는 아직 알 수 없다.하지만 여전히 작동하지는 않는다. 오류는 "색인 목록이 범위를 벗어났습니다"

path = "C:\sdmdownloads"   
    array = [] 
    for filename in glob.iglob(os.path.join(path, filelog_ext)): 
     with open(filename) as f: 
      array.extend(f) 
      self.mlb.insert (END, ('%s' % (array[0]),'%s' % (array[1]),'%s' % (array[2]),'%s' % (array[3]))) 
      self.mlb.pack (expand=YES,fill=BOTH) 
+0

파일에 4 줄이 없습니까? – jamylak

+0

첫 번째 줄의 첫 번째 토큰을 array [0]으로 가져 오거나 각 파일의 첫 줄을 array [0]으로 가져 오기를 기대합니까? –

답변

0

이와 비슷한 기능이 있습니까?

import glob 
import os 

filelog_ext=".log" #log extension 
array = [] 

for filename in glob.iglob(os.path.join(path, filelog_ext)): 
    with open(filename) as f: 
     array.extend(f) 
+0

array.extend (f)는 실제로 무엇을합니까? 코드를 업데이트했습니다. @John Zwink가 좋은지 말해주세요. – scandalous

+0

Extend는 목록의 끝 부분에서 인수의 모든 항목을 넣습니다. 따라서 루프가 완료되면 배열은 모든 파일의 모든 행을 갖게됩니다. –

+0

각 로그 파일에 4 개의 항목이 있으므로 배열 크기는 4 여야합니다. 또한 내 multilistbox 4 colums 이 가능 (2) 배열 으로 로그 파일에 4 선 복사 .log 파일 다음 (1) 개방을 반복하는 삽입되는 4 개 항목을 지원 (3) 내 멀티리스트 박스에 4 개의 배열 라인을 추가하십시오. (4)이 과정을 다시 반복하십시오. – scandalous