gpt4 book ai didi

python shutil.rmtree 抛出错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:59:02 28 4
gpt4 key购买 nike

有人可以向我解释为什么 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)

关于python shutil.rmtree 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26109244/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com