python 关闭文件后python无法删除文件,“被另一个进程使用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1856718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python can't remove a file after closing it, "being used by another process"
提问by alby
I am trying to remove a file after reading from it, but getting "WindowsError: [Error 32] The process cannot access the file because it is being used by another process"
我试图在读取文件后删除它,但收到“WindowsError:[错误 32] 该进程无法访问该文件,因为它正被另一个进程使用”
file = open(self.filePath)
for line in file:
#do things
file.close()
os.remove(self.filePath) #throws error
os.rename(self.filePath, self.filePath + "old") #throws same error
any ideas??
有任何想法吗??
thank you!
谢谢!
UPDATE: i just restarted my (windows 7) box, started eclipse, moved os.remove("c:\file\file.txt") to the first line of my script, ran and got the same error.
更新:我刚刚重新启动了我的(Windows 7)盒子,启动了 eclipse,将 os.remove("c:\file\file.txt") 移动到了我脚本的第一行,运行并得到了同样的错误。
UPDATE 2: i used windows explorer to navigate to the file.. first time I couldn't delete it, it was being used by "System". second attempt the delete worked fine. the file is no longer there. now when I run os.remove("c:\file\file.txt") WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\file\file.txt' then i look in windows explorer and the file is back, empty. i can delete in explorer, then when i run the script to remove, it throws the error (and creates the empty file). any idea what could be going on here?
更新 2:我使用 Windows 资源管理器导航到文件.. 第一次我无法删除它,它被“系统”使用。第二次尝试删除工作正常。该文件不再存在。现在当我运行 os.remove("c:\file\file.txt") WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\file\file.txt'然后我查看 Windows 资源管理器,文件又回来了,是空的。我可以在资源管理器中删除,然后当我运行脚本删除时,它会抛出错误(并创建空文件)。知道这里会发生什么吗?
UPDATE 3: i am an idiot. i was opening the file in the initialization of an object when i tested by moving the "remove" to top of script, i should have put it at the top, not after creating my objects
更新3:我是个白痴。当我通过将“删除”移动到脚本顶部进行测试时,我正在对象的初始化中打开文件,我应该将它放在顶部,而不是在创建我的对象之后
sorry for taking your time, i've been working on something all weekend that's due tomorrow and panicked when it wouldn't run
抱歉耽误了您的时间,我整个周末都在忙于明天到期的事情,并且在它无法运行时感到恐慌
回答by Greg Hewgill
Another possibilityis that a virus checker still has the file open at the time you try to delete or rename it. This doesn't happen often but when it does, it's really annoying to track down.
另一种可能性是,在您尝试删除或重命名文件时,病毒检查程序仍然打开了该文件。这种情况并不经常发生,但一旦发生,追踪起来真的很烦人。
回答by Tarnay Kálmán
Use Unlocker(easier) or Process Explorerto see what program has the file open.
使用Unlocker(更简单)或Process Explorer查看打开文件的程序。
(as suggested by Anon)
(如Anon所建议的)
回答by Tengis
I have the same problem right now with an access-AB. Process Explorer shows that .. 'python.exe' is using the file that I wish to delete. I can not kill Python. It is an annoying solution.
我现在用 access-AB 也有同样的问题。Process Explorer 显示 .. 'python.exe' 正在使用我希望删除的文件。我不能杀死 Python。这是一个烦人的解决方案。
回答by Paul Hoffman
There are lots of possible reasons for this. However, the most likely one is that you have a directory window open that contains the file, or that such a window was closed recently and Windows has yet to flush the directory from its cache. Try closing any directory window that you were looking in and try again.
这有很多可能的原因。但是,最可能的情况是您打开了一个包含该文件的目录窗口,或者最近关闭了这样一个窗口并且 Windows 尚未从其缓存中刷新该目录。尝试关闭您正在查看的任何目录窗口,然后重试。