2009-08-10 4 views
3

shutil.copy2를 광범위하게 사용하는 Python 스크립트가 있습니다.I/O 오류시 프로그램 종료 방지

Traceback (most recent call last): 
    File "run_model.py", line 46, in <module> 
    main() 
    File "run_model.py", line 41, in main 
    tracerconfigfile=OPT.tracerconfig) 
    File "ModelRun.py", line 517, in run 
    self.copy_data() 
    File "ModelRun.py", line 604, in copy_ecmwf_data 
    shutil.copy2(remotefilename, localfilename) 
    File "/usr/lib64/python2.6/shutil.py", line 99, in copy2 
    copyfile(src, dst) 
    File "/usr/lib64/python2.6/shutil.py", line 54, in copyfile 
    copyfileobj(fsrc, fdst) 
    File "/usr/lib64/python2.6/shutil.py", line 27, in copyfileobj 
    buf = fsrc.read(length) 
IOError: [Errno 5] Input/output error 

내 프로그램의 실행의 낙태를 방지하고 다시 시도 할 수 있습니다 방법 : 내가 네트워크를 통해 파일을 복사하는 데 사용하기 때문에, 나는 내 프로그램의 실행의 낙태로 이어질 너무 자주 I/O 오류를 얻을 대신 복사 프로세스?

이미 사용하고있는 코드는 파일이 실제로 파일 크기를 확인하여 완전하게 복사되는 것을 확인 :

def check_file(file, size=0): 
    if not os.path.exists(file): 
     return False 
    if (size != 0 and os.path.getsize(file) != size): 
     return False 
    return True 

while (check_file(rempdg,self._ndays*130160640) is False): 
    shutil.copy2(locpdg, rempdg) 

답변

7

어떤 블록 i 오류가 발생 했습니까? try/except 주위에 포장하십시오 :

def check_file(file, size=0): 
    try: 
     if not os.path.exists(file): 
      return False 
     if (size != 0 and os.path.getsize(file) != size): 
      return False 
     return True 
    except IOError: 
     return False # or True, whatever your default is 

while (check_file(rempdg,self._ndays*130160640) is False): 
    try: 
     shutil.copy2(locpdg, rempdg) 
    except IOError: 
     pass # ignore the IOError and keep going 
6

당신이 오류를 포착하고 그들에게

을 치료하기 위해

try: 
    ... 
except IOError as err: 
    ... 

을 사용할 수 있습니다

살펴보기 this