Windows错误:[Error 32] 进程无法访问文件,因为它正在被另一个进程使用:'new.dat'

3
以下是我的整个程序。我不知道哪里出了问题,因为它一直提示该进程正在被其他文件使用...我真的无法理解。请帮帮我,因为我需要在两天内提交。
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"

你的代码运行良好。不是这个问题。你是否删除了以前的 new.dat 文件?更改目标文件的名称,看看是否会运行。 - Shawn Mehan
@ShawnMehan .. 嗯,你是什么意思....所有的东西似乎都已经正确关闭了。 - IsMaTh IM
我的意思是,你的代码在另一台机器上运行,也就是我的机器上。因此,不是代码导致了你的问题,它在我的机器上运行良好。因此,你遇到的问题是机器和操作系统的问题,而不是你的程序问题。所以,你可以尝试更改文件名,以消除在该特定机器上可能存在的任何争用。 - Shawn Mehan
@ShawnMehan:这是一个交互式程序,程序流程取决于用户的输入。因此,如果没有使用与OP相同的输入,您的陈述就没有意义。 - miracle173
1个回答

1
尝试在不先关闭文件的情况下执行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

否则,如果您不想搞砸一切(请注意,如果没有错误发生,您的函数会保持文件打开状态),请尝试将每个except EOFError:块更改为finally:,最终结果与使用with相同。
从源代码中可以看到:
“try-finally结构保证在finally部分下的代码始终被执行,即使执行工作的代码没有完成。”
这应该确保关闭。

如果那不起作用,请在此处发表评论以保持辩论的开放。我们会找到另一种方法。 - user5457708

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接