2015-10-24 34 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

相关问题