2017-11-29 10 views
1
elif(ch==2): 
    fh=open("emp1.txt","rb+") 
    fo=open("temp.txt","wb+") 
    ecode=input("Enter the Ecode :") 
    rec=(" ") 
    try: 
     while True: 
      emp1= pickle.load(fh) 
      if (emp1.ecode!=ecode): 
       pickle.dump(emp1,fh) 

    except(EOFError): 
     fh.close() 
     fo.close() 
     os.remove("empl.txt") 
     os.rename("temp.txt","emp1.txt") 
     print("") 

다음 코드를 실행 내게이 오류 준다 : 위의 예외 처리하는 동안pickle.load에서 참고 EOFError 및 파일을 찾을 수 없음 오류

Traceback (most recent call last): File "C:\Users\hello\Desktop\bhavi\python programming\Employ.py", line 78, in emp1= pickle.load(fh) EOFError

을, 또 다른 예외가 발생했습니다 :

Traceback (most recent call last): File "C:\Users\hello\Desktop\bhavi\python programming\Employ.py", line 85, in os.remove("empl.txt") FileNotFoundError: [WinError 2] The system cannot find the file specified: 'empl.txt'

나는 지금 무엇을해야합니까 ??

답변

0

경로를 수정해야합니다. 첫 번째 경우에는 "emp1.txt"; 두 번째에는 "empl.txt"이라고 씁니다. 주의 깊게 보면 두 문자열에 차이가 있다는 것을 알아야합니다.

힌트 :'1' != 'l'

귀하의 코드는 아마뿐만 아니라 리팩토링 할 수있다. 매우 불완전하기 때문에 다른 사람들이 코드를 테스트 할 수는 없지만, 다음은 그 자리에서 작동해야합니다. 그래도 작동하는지 확인해야합니다.

elif ch == 2: 
    with open('emp1.txt', 'rb+') as fh, open('temp.txt', 'wb+') as fo: 
     ecode = input('Enter the Ecode: ') 
     while True: 
      try: 
       item = pickle.load(fh) 
      except EOFError: 
       break 
      else: 
       if item.ecode != ecode: 
        pickle.dump(item, fo) 
    os.remove(fh.name) 
    os.rename(fo.name, fh.name) 
    print() 
0

나는 shelve를 사용합니다. 사용하기가 훨씬 쉬워서 제 경험에서 많은 오류가 발생하지 않습니다. 선반은 피클에 내장되어 있지만 단순화했습니다. 여기

짧은 튜토리얼

http://python.wikia.com/wiki/Shelve

에게 있습니다