WindowsError:[错误32]该进程无法访问文件,因为该文件正在被另一个进程使用:"new.dat" [英] WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'new.dat'

查看:192
本文介绍了WindowsError:[错误32]该进程无法访问文件,因为该文件正在被另一个进程使用:"new.dat"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

the below is my entire program .. i just don't know were am I going wrong it keeps on telling that the process is eing used by some other file ...I just don't understand please help me out as I need to submit this within two days time

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"

推荐答案

尝试先f1=open("new.dat","rb")然后os.remove("new.dat")而不先关闭它;您无法删除它,并且会发生错误,因为该文件处于另一个进程"下,换句话说,它正在读取中

Try to f1=open("new.dat","rb") and then os.remove("new.dat") without closing it first; You cannot delete it and the error occures because the file is under 'another process', in other words it's under reading

您永远不要使用f = open(filename, 'x')打开文件,然后使用f.close()关闭文件,如果由于某种原因该程序不执行关闭操作,它可能会产生与您完全相同的问题...

You shouldn't ever open the file with f = open(filename, 'x') and then close with f.close(), it might create problems exactly like yours if for some reason the program doesn't execute the closure...

相反,尝试使用with open(filename, 'x') as f:重写您的函数,当其中的代码运行完毕或出现错误而程序不受影响时,它将自动关闭文件

Instead, try to rewrite your functions using with open(filename, 'x') as f:, it automatically closes the file when the code within has finished to run or on error without the program being affected

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

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

Otherwise, if you don't want to mess everything up (note that your functions keep the the files open if error doesn't occur), try to change every except EOFError: block with finally:, which, at the end, is the same as using with.

来源:

"try-finally结构保证finally部分下的代码 即使执行工作的代码无法执行,也始终会执行 完成."

"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."

这应该确保关闭

这篇关于WindowsError:[错误32]该进程无法访问文件,因为该文件正在被另一个进程使用:"new.dat"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆