使用Python删除整个目录树 | shutil.rmtree() 方法
Python中的Shutil 模块提供了许多对文件和文件集合进行高级操作的功能。它属于 Python 的标准实用程序模块。该模块有助于自动化复制和删除文件和目录的过程。
shutil.rmtree() 用于删除整个目录树,路径必须指向目录(但不能指向目录的符号链接)。
Syntax: shutil.rmtree(path, ignore_errors=False, onerror=None)
Parameters:
path: A path-like object representing a file path. A path-like object is either a string or bytes object representing a path.
ignore_errors: If ignore_errors is true, errors resulting from failed removals will be ignored.
oneerror: If ignore_errors is false or omitted, such errors are handled by calling a handler specified by onerror.
示例 1:假设目录和子目录如下。
# 父目录:
# 父目录内的目录:
# 子目录下的文件:
我们要删除目录作者。下面是实现。
Python3
# Python program to demonstrate
# shutil.rmtree()
import shutil
import os
# location
location = "D:/Pycharm projects/GeeksforGeeks/"
# directory
dir = "Authors"
# path
path = os.path.join(location, dir)
# removing directory
shutil.rmtree(path)
Python3
# Python program to demonstrate
# shutil.rmtree()
import shutil
import os
# location
location = "D:/Pycharm projects/GeeksforGeeks/"
# directory
dir = "Authors"
# path
path = os.path.join(location, dir)
# removing directory
shutil.rmtree(path, ignore_errors = False)
# making ignore_errors = True will not raise
# a FileNotFoundError
Python3
# Python program to demonstrate
# shutil.rmtree()
import shutil
import os
# exception handler
def handler(func, path, exc_info):
print("Inside handler")
print(exc_info)
# location
location = "D:/Pycharm projects/GeeksforGeeks/"
# directory
dir = "Authors"
# path
path = os.path.join(location, dir)
# removing directory
shutil.rmtree(path, onerror = handler)
输出:
示例 2:通过传递 ignore_errors = False。
Python3
# Python program to demonstrate
# shutil.rmtree()
import shutil
import os
# location
location = "D:/Pycharm projects/GeeksforGeeks/"
# directory
dir = "Authors"
# path
path = os.path.join(location, dir)
# removing directory
shutil.rmtree(path, ignore_errors = False)
# making ignore_errors = True will not raise
# a FileNotFoundError
输出:
Traceback (most recent call last):
File “D:/Pycharm projects/gfg/gfg.py”, line 16, in
shutil.rmtree(path, ignore_errors=False)
File “C:\Users\Nikhil Aggarwal\AppData\Local\Programs\Python\Python38-32\lib\shutil.py”, line 730, in rmtree
return _rmtree_unsafe(path, onerror)
File “C:\Users\Nikhil Aggarwal\AppData\Local\Programs\Python\Python38-32\lib\shutil.py”, line 589, in _rmtree_unsafe
onerror(os.scandir, path, sys.exc_info())
File “C:\Users\Nikhil Aggarwal\AppData\Local\Programs\Python\Python38-32\lib\shutil.py”, line 586, in _rmtree_unsafe
with os.scandir(path) as scandir_it:
FileNotFoundError: [WinError 3] The system cannot find the path specified: ‘D:/Pycharm projects/GeeksforGeeks/Authors’
示例 3:通过传递 onerror。
在 onerror 中,应该传递一个必须包含三个参数的函数。
- 函数:引发异常的函数。
- 路径:传递的路径名,在删除时引发异常
- excinfo: sys.exc_info() 引发的异常信息
下面是实现。
Python3
# Python program to demonstrate
# shutil.rmtree()
import shutil
import os
# exception handler
def handler(func, path, exc_info):
print("Inside handler")
print(exc_info)
# location
location = "D:/Pycharm projects/GeeksforGeeks/"
# directory
dir = "Authors"
# path
path = os.path.join(location, dir)
# removing directory
shutil.rmtree(path, onerror = handler)
输出:
Inside handler
(, FileNotFoundError(2, ‘The system cannot find the path specified’), )
Inside handler
(, FileNotFoundError(2, ‘The system cannot find the file specified’), )