有人可以向我解释为什么 shutil.rmtree 抛出错误说目录不为空吗?
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Python27\lib\threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "W:\__init__.py", line 90, in makePatch
myprog.copy_data()
File "W:\myprog.py", line 143, in copy_data
self.cleanupTempDir()
File "W:\myprog.py", line 138, in cleanupTempDir
shutil.rmtree(self.TEMP_DIR)
File "C:\Python27\lib\shutil.py", line 247, in rmtree
rmtree(fullname, ignore_errors, onerror)
File "C:\Python27\lib\shutil.py", line 256, in rmtree
onerror(os.rmdir, path, sys.exc_info())
File "C:\Python27\lib\shutil.py", line 254, in rmtree
os.rmdir(path)
WindowsError: [Error 145] The directory is not empty: u'e:\\PatchData\\Data'
请您参考如下方法:
Linux:我在处理临时文件夹时也遇到了这个错误:我在 __init__ 构造函数中调用了 os.makedirs和 __del__ 解构器中的 shutil.rmtree 调用,该代码使用了 multiprocessing 库;该文件夹是按顺序构建的,但只要进程终止执行就被销毁。因此,该错误并不总是可重现的,但据推测,只有当两个进程试图同时销毁该文件夹并且没有适当的锁定机制时,才会抛出这些错误。发生这种情况是因为出于某种未知原因,多次调用了 __del__ 解构函数。
错误如下:
Exception ignored in: <function VideoMaker.__del__ at 0x7f6066a888b0>
Traceback (most recent call last):
File "VideoMaker.py", line 49, in __del__
onerror(os.unlink, fullname, sys.exc_info())
File "/home/Marco/miniconda3/lib/python3.8/shutil.py", line 670, in _rmtree_safe_fd
onerror(os.rmdir, path, sys.exc_info())
File "/home/Marco/miniconda3/lib/python3.8/shutil.py", line 717, in rmtree
_rmtree_safe_fd(fd, path, onerror)
shutil.rmtree(self.temp_videoclip_dir)
File "/home/Marco/miniconda3/lib/python3.8/shutil.py", line 672, in _rmtree_safe_fd
File "/home/Marco/miniconda3/lib/python3.8/shutil.py", line 715, in rmtree
onerror(os.unlink, fullname, sys.exc_info())
File "/home/Marco/miniconda3/lib/python3.8/shutil.py", line 670, in _rmtree_safe_fd
os.unlink(entry.name, dir_fd=topfd)
FileNotFoundError: [Errno 2] No such file or directory: '12.mp4'
或者
OSError: [Errno 39] Directory not empty: 'tmp/tmp.videoclips/'
不要在 __del__ 解构器中使用 rmtree:
对象解构器可能并不总是具有可预测的行为,因此避免在内部使用操作系统的 IO 操作。创建一个单独的函数,并在对象范围的末尾调用它。
如果使用多处理或线程:LOCK
文档:
文档示例: Synchronization between processes
l.acquire()
try:
if os.path.isdir(path_to_delete):
shutil.rmtree(path_to_delete)
finally:
l.release()
如果真的没有别的办法:
def try_to_rmtree(can_still_retry):
if can_still_retry:
try:
if os.path.isdir(path_to_delete):
shutil.rmtree(path_to_delete)
except:
try_to_rmtree(can_still_retry -1)
can_still_retry = 5
try_to_rmtree(can_still_retry)
