错误 32:无法在 Python 脚本中重命名文件

Error 32: Can't rename file in Python Script

这是我的错误:

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

我从工作线程调用下面的函数,当它完成它的长 运行 过程时。它停止线程,然后在显示对话框和重命名文件之前给它 10 秒。 "Unlocker" 显示文件仅在 Python.exe.

中打开
 def allDone(self, event):
    myClass.worker.stop()
    for i in range(0,10):
        time.sleep(1)
        print(i)
    dlg = wx.MessageBox("All done!", "Ask Alfred", wx.OK | wx.ICON_INFORMATION)
    os.rename(self.tempf, self.tempf+"123.xls")
    self.Destroy()

文件在工作线程中通过以下 xlrd 方法在代码中打开:

rbook=xlrd.open_workbook(self.file)
sheet = rbook.sheet_by_index(0)
wbook = copy(rbook)

文件需要在所有进程中关闭,甚至 "Python.exe" 才能移动它。

question 似乎您需要让此 xlrd 对象超出范围,以便在您尝试移动和重命名文件之前它会自行关闭。

如果您在线程中引用了该文件,则应尝试将变量设置为 None,以便在处理完成时删除对该文件对象的引用。希望这会解决您的问题