我收到WindowsError:[Error 32]该进程无法访问该文件,因为它正在被 except block 中的另一个进程使用
。怎么会发生这种事?
try:
shutil.move(os.path.join(folder, name), folder)
except shutil.Error:
# Handling Error: Destination already exists
unique_name = "%s_%s" %(name, time.time())
shutil.move(os.path.join(folder, name), os.path.join(folder, unique_name))
假设我可以在 Windows 中处理这样的异常(我希望第一个 shutil.move
在出现异常时不会锁定文件或任何内容),它是否像 Shutil.Error
在 try block 中引发,因为文件已经存在,并且它进入 except block ,在该 block 中它尝试使用新文件名移动它,却发现它被另一个进程使用?
如果我将 shutdown.move 操作编写为 Python 线程,是否有可能两个线程尝试执行相同的操作?或者这不会发生,因为有 GIL 锁强制顺序执行?
在Windows中,一般情况下,我可以同时打开两个文件进行写入吗?
最佳答案
Or that won't happen because there is a GIL lock making is happen in sequence?
尽管线程按顺序发生,但这并不意味着它们会放弃文件处理程序,这意味着当使用文件的线程不活动时,文件仍然处于锁定状态。
is it like shutil.Error happened in the try block because the file already existed
如果您的 shutil.move
都失败,则意味着问题出在读取源文件,而不是写入目标文件。还有另一个进程(或者可能是您自己的 Python 进程中的较早进程?)已打开该文件,并且尚未撤销对该文件的读取锁定。
关于python - Windows错误: [Error 32] when I do shutil.移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34499362/