我注意到在关闭代码之前是不可能删除任何文件的,在这个文件被使用的地方,即使在 .close() 之后也是如此。我在堆栈溢出中看到了类似的问题,但我仍然无法理解这个问题。如果你告诉我我的问题,我会非常感激。
import os
with open ("test.txt", "r") as fl:
print(fl.read())
if fl.closed:
os.remove("test.txt")
else:
print("It isn't closed")
要么
import os
fname = "test.txt"
fl = open(fname)
print(fl.read())
fl.close()
if fl.closed:
os.remove("test.txt")
else:
print("It isn't closed")
同样的错误:“PermissionError: [WinError 32] 该进程无法访问该文件,因为它正被另一个进程使用”
好的,伙计们,它通过多次重新启动 Spyder 以某种方式自行解决了。谢谢,祝你好运
嘿你可以使用unlink
和使用a+
import os
with open ("test.txt", "a+") as fl:
print(fl.read())
if fl.closed:
os.unlink("test.txt")
else:
print("It isn't closed")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句