2015-10-24 36 views
1

以下是我的整個程序..我只是不知道我是不是錯了 它不斷告訴進程正在eing使用其他一些文件...我只是不明白 請幫助我,因爲我需要兩天的時間內提交該WindowsError:[錯誤32]該進程無法訪問該文件,因爲它正在被另一個進程使用:'new.dat'

import os 
import pickle 
password=123 
l=input("Enter Password :") 
if l==password: 
    print"--------Welcome Admin--------" 
    class new: 
     def __init__(self,a,b,c): 
      self.acno=a 
      self.acna=b 
      self.ini=c 
     def display(self): 
      print self.acno,self.acna,self.ini 
    def form(): # creating new account holders 
     print"  ---New Account Entry Form--- " 
     n=input("Enter the no. Forms") 
     f1=open("new.dat","ab") 
     for i in range (n): 
      a=input("Enter The Account No:") 
      b=raw_input("Enter The Name of The Account Holder:") 
      c=input("Enter The Initial Amount (>=5000):") 
      if c<5000: 
       print "Initial Amount Too Low" 
       c=input("Enter The Initial Amount (>=5000):") 
      e=new(a,b,c) 
      pickle.dump(e,f1) 
     f1.close() 
     print"--------Account Created Successfully--------------" 
    def depo():#depositing amount in the account 
     print" ---- Account Deposition Form ---- " 
     p=input("Enter the Account Number:") 
     f1=open("new.dat","rb") 
     f2=open("dep.dat","wb") 
     amt=input("Enter the Amount to Deposit :") 
     try: 
      while True: 
       s=pickle.load(f1) 
       if s.acno==p: 

        s.ini+=amt 
        pickle.dump(s,f2) 
     except EOFError: 
      f1.close() 
      f2.close() 
     print"Amount Deposited Successfully" 
     os.remove("new.dat") 
     os.rename("dep.dat","new.dat") 
    def withdraw():#to withdraw 
     print "   Account Transaction Form   " 
     p=input("enter the account n:") 
     f2=open("new.dat","rb") 
     f3=open("f2.dat","wb") 
     amt=input("Enter the amount to Withdraw:") 
     try: 
      while True: 
       s=pickle.load(f2) 
       if s.acno==p: 
        if s.ini>amt or s.ini==amt : 
         s.ini-=amt 
         pickle.dump(s,f3) 
         print "Amount Withdrawed" 
        elif s.ini<amt: 
         print "No sufficient balance " 
        else: 
         print " Account no. invalid" 
     except EOFError: 
      f2.close() 
      f3.close() 
     os.remove("new.dat") 
     os.rename("f2.dat","new.dat") 
    def balance():#check the balance 
     print"   Balance Amount Details" 
     p=input("Enter the Account Number:") 
     f3=open("new.dat","rb") 
     try: 
      while True: 
       s=pickle.load(f3) 
       if s.acno==p: 
        print "the Balance Amount for Acc. no. ",p,"is :", s.ini 
     except EOFError: 
      f3.close() 
    def displa():#display all the account holders 
     print "  All Account Holders List  " 
     f1=open("new.dat","rb") 
     try : 
      while True: 
       k=pickle.load(f1) 
       k.display() 
     except EOFError: 
      f1.close() 
    def dele(): # to delete the account holder error here 
     print "  Account Deletion Form" 
     f1=open("new.dat","rb") 
     f2=open("tnew.dat","wb") 
     try: 
      e=pickle.load(f1) 
      no=input("Enter the Account No:") 
      if e.acno!=no: 
       pickle.dump(e,f2) 
     except EOFError: 
      f2.close() 
      f1.close() 
     os.remove("new.dat") #error here 
     os.rename("tnew.dat","new.dat") # error here 
     f1.close() 
    while True: 
     print"-----------------------------------------------------------------------------------------------------------------" 
     print"                    Bank Management System" 
     print"                          " 
     print"---Main Menu--- :" 
     print"1. New account" 
     print"2. Deposit amount" 
     print"3. Withdraw amount" 
     print"4. Balance account" 
     print"5. All Account Holders List " 
     print"6. Close An Account" 
     print"7. Exit" 
     choice=input("Enter Your Option (1-7):") 
     if choice==1: 
      form() 
     elif choice==2: 
      depo() 
     elif choice==3: 
      withdraw() 
     elif choice==4: 
      balance() 
     elif choice==5: 
      displa() 
     elif choice==6: 
      dele() 
     else: 
      print "-------Login In Later----------" 
      break 
else: 
    print "PASSWORD Incorrect" 
+0

您的代碼運行正常。不是的。你有沒有刪除任何以前的'new.dat'?更改目標文件的名稱,然後查看它是否會運行。 –

+0

@ShawnMehan ..嗯你是什麼意思....所有似乎都正確關閉 –

+0

我的意思是,你的代碼運行在另一臺機器上,即我的。所以它不是導致你的問題的代碼。它在我的機器上運行。因此,您的機器+操作系統出現問題,而不是您的程序。因此,您可以嘗試更改文件的名稱,以消除該特定計算機上可能存在的特定文件的爭用...。 –

回答

0

嘗試f1=open("new.dat","rb")然後os.remove("new.dat")不先關閉它;你不能刪除它,因爲該文件在'另一個進程'下,換句話說它正在讀取

你不應該用f = open(filename, 'x')打開文件,然後關閉f.close(),它可能會產生完全一樣的問題你的,如果由於某種原因該程序不會執行關閉...

而是,嘗試使用with open(filename, 'x') as f:重寫您的功能,它會自動關閉文件,當內部代碼已完成運行或發生錯誤,沒有程序受影響

http://effbot.org/zone/python-with-statement.htm

否則,如果你不想把所有東西搞糟 (注意你的函數保持文件打開,如果沒有錯誤發生), 試圖改變每塊與finally:,最後,它是與使用with相同。

從來源:

"The try-finally construct guarantees that the code under the finally part is always executed, even if the code that does the work doesn’t finish."

這應保證關閉

+0

如果這不起作用,請在此留言以保持辯論暢通。我們會找到另一種方式 – Mark

相關問題