【问题标题】:WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'new.dat'WindowsError:[错误 32] 该进程无法访问该文件,因为它正在被另一个进程使用:'new.dat'
【发布时间】:2022-03-16 03:06:15
【问题描述】:

以下是我的整个程序.. 我只是不知道我是不是错了 它一直在告诉其他文件正在使用该进程...我只是不明白 请帮帮我,因为我需要在两天内提交此文件

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?更改目标文件的名称,看看它是否会运行。
  • @ShawnMehan .. 嗯,你是什么意思.. 一切似乎都已正确关闭
  • 我的意思是,您的代码在另一台机器上运行,即我的。因此,导致您的问题的不是代码。它在我的机器上运行。因此,您的机器+操作系统有问题,而不是您的程序。因此,您可以尝试更改文件的名称,以消除该特定机器上该特定文件可能存在的任何争用......
  • @ShawnMehan:这是一个非交互式程序,程序流程取决于用户的输入。因此,如果不使用与 OP 相同的输入,您的陈述就没有意义。

标签: python file


【解决方案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

否则,如果您不想搞砸一切 (请注意,如果没有发生错误,您的函数会保持文件打开), 尝试使用finally: 更改每个except EOFError: 块,最后与使用with 相同。

来源:

"try-finally 构造保证finally 部分下的代码 总是被执行,即使完成工作的代码没有 完成。”

这应该确保关闭

【讨论】:

  • 如果这不起作用,请在此处发表评论以保持辩论的畅通。我们会找到另一种方式
猜你喜欢
  • 2018-10-28
  • 1970-01-01
  • 2015-11-13
  • 2015-01-28
  • 1970-01-01
  • 1970-01-01
  • 2017-06-02
  • 2021-09-21
  • 2019-11-26
相关资源
最近更新 更多