WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'new.dat'

前端 未结 1 682
南旧
南旧 2020-12-22 04:15

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

相关标签:
1条回答
  • 2020-12-22 04:52

    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

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

    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

    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.

    From source:

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

    This should assure the closure

    0 讨论(0)
提交回复
热议问题