无法使用os.remove()删除文件

时间:2019-01-22 12:34:33

标签: python jupyter-notebook

只需尝试文件IO操作。 os.remove()函数无法删除文件 下面的代码

file=open("test.txt","r+")
text=file.read()
print(text)
file.close()
os.remove("test.txt")

PermissionError                           Traceback (most recent call last)
<ipython-input-10-122dc088faef> in <module>
      4 file.close()
      5 #youcantdeleteifyoudontcloseit
----> 6 os.remove("test.txt")

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'test.txt'

1 个答案:

答案 0 :(得分:1)

  

无法在任何地方打开

错误消息明确表明文件已在某处打开。即使对于Windows,也有可用的工具可以找到打开文件的进程,例如Sysinternals Process Explorer。