2011-10-23 4 views
1

처음으로 파이썬 프로그램을 작성했습니다. 로컬 폴더에 저장된 우편물에 zip 파일을 첨부합니다. 프로그램은 새 파일이 있는지 확인하고 파일이 있으면 zip 파일을 추출하고 파일 이름을 기반으로 다른 폴더로 추출합니다. 내 코드를 실행할 때 다음 오류가 발생합니다." 'NoneType'개체가 반복 가능하지 않습니다."오류

Traceback (최근 호출 마지막) : 파일 "C : /Zip/zipauto.py", 28 행, new_files의 파일 : TypeError : 'NoneType'개체 iterable이 아닙니다

아무도 내가 잘못 가고있는 곳을 말해 줄 수 있습니까?

시간 내 주셔서 감사 많은,

나빈 여기 내 코드입니다 :

import zipfile 
import os 

ROOT_DIR = 'C://Zip//Zipped//' 
destinationPath1 = "C://Zip//Extracted1//" 
destinationPath2 = "C://Zip//Extracted2//" 

def check_for_new_files(path=ROOT_DIR): 

    new_files=[] 
    for file in os.listdir(path): 
     print "New file found ... ", file 

def process_file(file): 

    sourceZip = zipfile.ZipFile(file, 'r') 
    for filename in sourceZip.namelist(): 
      if filename.startswith("xx") and filename.endswith(".csv"): 
        sourceZip.extract(filename, destinationPath1) 
      elif filename.startswith("yy") and filename.endswith(".csv"): 
        sourceZip.extract(filename, destinationPath2) 
        sourceZip.close() 

if __name__=="__main__": 
    while True: 
      new_files=check_for_new_files(ROOT_DIR) 
      for file in new_files: # fails here 
        print "Unzipping files ... ", file 
        process_file(ROOT_DIR+"/"+file) 

답변

6

check_for_new_filesreturn statement이 없습니다, 따라서 묵시적 없음을 반환하지 않습니다. 따라서,

new_files=check_for_new_files(ROOT_DIR) 

세트 None에 new_files, 당신은 None 반복 할 수 없다. check_for_new_files에서

돌아 읽기 파일 :

여기
def check_for_new_files(path=ROOT_DIR): 
    new_files = os.listdir(path) 
    for file in new_files: 
     print "New file found ... ", file 
    return new_files 
+0

aha..that 해결해 주셔서 감사합니다. – Navin

1

은 다음 두 질문에 대한 대답입니다 :

(1) while True: : 코드는 루프를 영원히 것이다.

(2) 사용자의 기능 check_for_new_files이 새 파일을 확인하지 않으면 파일을 확인합니다. 들어오는 각 파일을 처리 한 후에 아카이브 디렉토리로 이동하거나 일종의 타임 스탬프 메커니즘을 사용해야합니다.

+0

예 .. shutil 모듈을 사용하여 들어오는 파일을 처리 한 후 다른 디렉토리로 이동했습니다. – Navin

관련 문제