python: delete non-empty dir
本问题已经有最佳答案,请猛点这里访问。
如何删除python中可能不为空的dir。
目录可能有许多层次的嵌套子目录。
使用
1 2 3 | import shutil shutil.rmtree(path) |
有关如何处理和/或忽略错误的详细信息,请参阅文档。
标准库为此包含shutil.rmtree。默认情况下,
1 | shutil.rmtree(path) # errors if dir not empty |
将给予
您可以忽略以下错误删除目录及其内容:
1 | shutil.rmtree(role_fs_path, ignore_errors=True) |
您还可以通过传递
你想要舒特尔.rmtree
shutil.rmtree(path[, ignore_errors[,
onerror]])Delete an entire directory
tree; path must point to a directory
(but not a symbolic link to a
directory). If ignore_errors is true,
errors resulting from failed removals
will be ignored; if false or omitted,
such errors are handled by calling a
handler specified by onerror or, if
that is omitted, they raise an
exception.