【发布时间】:2019-09-05 11:35:04
【问题描述】:
我有一个目录,目录树中的任何位置都可以有.unwanted 目录。我想删除这些。
import shutil
shutil.rmtree('.unwanted', onerror=True)
这不起作用,因为目录是隐藏的。输出:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 374, in _rmtree_unsafe
with os.scandir(path) as scandir_it:
FileNotFoundError: [WinError 3] The system cannot find the path specified: '.unwanted'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/SYSTEM/CODING/PYTHON/import.py", line 31, in <module>
shutil.rmtree('.unwanted', onerror=True)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 516, in rmtree
return _rmtree_unsafe(path, onerror)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 377, in _rmtree_unsafe
onerror(os.scandir, path, sys.exc_info())
TypeError: 'bool' object is not callable
Process finished with exit code 1
别管行号,示例代码来自一个更大的脚本。
【问题讨论】:
-
您确定您在正确的目录中吗?最好使用绝对路径。
-
工作目录是
e:\import,但.unwanted目录位于未知目录内的某处。 -
那么你必须使用os.walk,shutil.rmtree 不会神奇地删除你工作目录下的所有子目录
标签: python python-3.x