Trong bài viết này, chúng tôi sẽ đề cập đến cách xóa [xóa] các tệp và thư mục trong Python. Python cung cấp các phương pháp và chức năng khác nhau để xóa các tệp và thư mục. Người ta có thể xóa tệp theo nhu cầu của họ. & NBSP;
Các phương pháp khác nhau được cung cấp bởi Python là -
- Sử dụng OS.Remove []
- Sử dụng OS.RMDIR []
- Sử dụng swutil.rmtree []
- Sử dụng pathlib.path [dlank_dir_path] .rmdir []
Xóa tệp/Dir bằng phương thức Os.Remove []
Mô -đun HĐH trong Python cung cấp các chức năng để tương tác với hệ điều hành. Tất cả các chức năng trong mô -đun HĐH đều tăng Oserror trong trường hợp tên và đường dẫn tệp không hợp lệ hoặc không thể truy cập hoặc các đối số khác có loại chính xác nhưng không được hệ điều hành chấp nhận. & NBSP; in Python provides functions for interacting with the operating system. All functions in the os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted by the operating system.
Phương thức OS.Remove [] trong Python được sử dụng để xóa hoặc xóa đường dẫn tệp. Phương pháp này không thể xóa hoặc xóa một thư mục. Nếu đường dẫn được chỉ định là một thư mục thì Oserror sẽ được đưa ra bằng phương pháp. is used to remove or delete a file path. This method can not remove or delete a directory. If the specified path is a directory then OSError will be raised by the method.
Cú pháp của Os.Remove []
Cú pháp: os.remove [đường dẫn, *, dir_fd = none] & nbsp; os.remove[path, *, dir_fd = None]
Tham số: Đường dẫn: Một đối tượng giống như đường dẫn biểu thị đường dẫn tệp. Một đối tượng giống như đường dẫn là một đối tượng chuỗi hoặc byte đại diện cho một đường dẫn. & Nbsp; path: A path-like object representing a file path. A path-like object is either a string or bytes object representing a path.
- DIR_FD [Tùy chọn]: Một mô tả tệp đề cập đến một thư mục. Giá trị mặc định của tham số này là không có. Nếu đường dẫn được chỉ định là tuyệt đối thì dir_fd bị bỏ qua. & Nbsp;A file descriptor referring to a directory. The default value of this parameter is None. If the specified path is absolute then dir_fd is ignored.
Lưu ý: Danh sách tham số ‘*trong danh sách tham số chỉ ra rằng tất cả các tham số sau [ở đây trong trường hợp của chúng tôi‘ DIR_FD,] là các tham số chỉ dành cho từ khóa và chúng có thể được cung cấp bằng tên của chúng, không làm tham số vị trí. & NBSP; The ‘*’ in parameter list indicates that all following parameters [Here in our case ‘dir_fd’] are keyword-only parameters and they can be provided using their name, not as positional parameter.
Loại trả về: Phương thức này không trả về bất kỳ giá trị nào.This method does not return any value.
Ví dụ 1: Xóa một tệp trong Python
Giả sử tệp có trong thư mục là: & nbsp;
Chúng tôi muốn xóa File1 khỏi thư mục trên. Dưới đây là việc thực hiện. & NBSP;
Python3
import
os
file
=
'file1.txt'
location
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed1
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed2
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4
file
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed7
Output:
Ví dụ 2: Xóa tệp bằng đường dẫn tuyệt đối
Nếu đường dẫn được chỉ định là một thư mục. & Nbsp;
Python3
import
os
file
=
'file1.txt'
location
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed1
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed2
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4
file
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed7
Ví dụ 2: Xóa tệp bằng đường dẫn tuyệt đối
Output:
Traceback [most recent call last]: File "osremove.py", line 11, in os.remove[path] IsADirectoryError: [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
Nếu đường dẫn được chỉ định là một thư mục. & Nbsp; Check if File Exists Before Deleting
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed0
=
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed2
Python3
import
os
file
=
'file1.txt'
import
3import
4
import
5
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed7
location
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed1
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed2
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4
file
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6
import
5
Deleted 'Untitled Folder' successfully2
os
7import
5
Deleted 'Untitled Folder' successfully2
Deleted 'Untitled Folder' successfully3
file
1[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6
Output:
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed
Ví dụ 2: Xóa tệp bằng đường dẫn tuyệt đốiTo know more about os.remove[] click here.
Nếu đường dẫn được chỉ định là một thư mục. & Nbsp;
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed0
=
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed2OSError will be raised if the specified path is not an empty directory.
location
=
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
5
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed2=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed0[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6os.rmdir[path, *, dir_fd = None]Parameter:
Deleted 'Untitled Folder' successfully2Deleted 'Untitled Folder' successfully3Deleted 'Untitled Folder' successfully4Deleted 'Untitled Folder' successfully5[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed0[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6 A path-like object representing a file path. A path-like object is either a string or bytes object representing a path.- DIR_FD [Tùy chọn]: Một mô tả tệp đề cập đến một thư mục. Giá trị mặc định của tham số này là không có. Nếu đường dẫn được chỉ định là tuyệt đối thì dir_fd bị bỏ qua. & Nbsp; A file descriptor referring to a directory. The default value of this parameter is None. If the specified path is absolute then dir_fd is ignored.
Lưu ý: Danh sách tham số ‘*trong danh sách tham số chỉ ra rằng tất cả các tham số sau [ở đây trong trường hợp của chúng tôi‘ DIR_FD,] là các tham số chỉ dành cho từ khóa và chúng có thể được cung cấp bằng tên của chúng, không làm tham số vị trí. & NBSP; The ‘*’ in parameter list indicates that all following parameters [Here in our case ‘dir_fd’] are keyword-only parameters and they can be provided using their name, not as positional parameter.
Loại trả về: Phương thức này không trả về bất kỳ giá trị nào. This method does not return any value.
Ví dụ 1: Xóa một tệp trong Python Delete all directories from a Directory
Giả sử tệp có trong thư mục là: & nbsp;
Chúng tôi muốn xóa File1 khỏi thư mục trên. Dưới đây là việc thực hiện. & NBSP;
Python3
import
os
file
=
'file1.txt'
location
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed1
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed2
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4
file
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6
=
4
Output:
Ví dụ 2: Xóa tệp bằng đường dẫn tuyệt đối
Nếu đường dẫn được chỉ định là một thư mục. & Nbsp;
Python3
import
os
file
=
'file1.txt'
location
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed1
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed2
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4
file
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6
import
3import
4
Ví dụ 2: Xóa tệp bằng đường dẫn tuyệt đối
Nếu đường dẫn được chỉ định là một thư mục. & Nbsp;
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed2
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4
file
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6
import
5
Deleted 'Untitled Folder' successfully2
os
7Ví dụ 2: Xóa tệp bằng đường dẫn tuyệt đối
Output:
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed
Nếu đường dẫn được chỉ định là một thư mục. & Nbsp; To know more about os.rmdir[] click here.
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
0 =
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
2
location
=
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed5
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
2=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
4[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
0[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
6
Deleted 'Untitled Folder' successfully2Deleted 'Untitled Folder' successfully3Deleted 'Untitled Folder' successfully4Deleted 'Untitled Folder' successfully5[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed0[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6 shutil.rmtree[path, ignore_errors=False, onerror=None]Parameters:
Deleted 'Untitled Folder' successfully2Deleted 'Untitled Folder' successfully3Deleted 'Untitled Folder' successfully4Deleted 'Untitled Folder' successfully5[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed0[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6A path-like object representing a file path. A path-like object is either a string or bytes object representing a path.- bỏ qua_errors: Nếu bỏ qua_errors là đúng, các lỗi do loại bỏ không thành công sẽ bị bỏ qua. & nbsp; If ignore_errors is true, errors resulting from failed removals will be ignored.
- Oneerror: Nếu bỏ qua_error là sai hoặc bị bỏ qua, các lỗi đó được xử lý bằng cách gọi một trình xử lý được chỉ định bởi Onerror. If ignore_errors is false or omitted, such errors are handled by calling a handler specified by onerror.
Xóa một thư mục và các tệp có trong đó.
Ví dụ 1: & nbsp;
Giả sử thư mục và các thư mục phụ như sau.
& nbsp;# thư mục cha mẹ: & nbsp;# Parent directory:
& nbsp;# thư mục bên trong thư mục mẹ: & nbsp;# Directory inside parent directory:
# Tệp bên trong thư mục phụ: & nbsp;
Ví dụ: Xóa tất cả các tệp khỏi thư mục
Chúng tôi muốn loại bỏ các tác giả thư mục. Dưới đây là việc thực hiện. & NBSP;
Python3
import
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed08
import
os
location
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed13
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed0
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed16
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed2
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed0
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed22
Output:
Ví dụ 2: Bỏ qua lỗi trong khi xóa thư mục
Bằng cách vượt qua bỏ qua_errors = true. & Nbsp;
Python3
import
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed08
import
os
location
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed13
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed0
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed16
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed2
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed0
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed38
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed40
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6
Output:
Ví dụ 2: Bỏ qua lỗi trong khi xóa thư mục
Bằng cách vượt qua bỏ qua_errors = true. & Nbsp;
Traceback [cuộc gọi gần đây nhất cuối cùng]: File \ Chương trình \ Python \ Python38-32 \ lib \ shatil.py, dòng 730, trong rmtree return _rmtree_unsafe [path, onerror] File lib \ swutil.py, dòng 589, trong _rmtree_unsafe onerror [os.scandir, path, sys.exc_info []] Shutil.py, dòng 586, trong _rmtree_unSafe với Os.Scandir [đường dẫn] dưới dạng scandir_it: filenotfounderror: [WinError 3] Hệ thống không thể tìm thấy đường dẫn được chỉ định: '
- Ví dụ 3: Trình xử lý ngoại lệfunction which raised the exception.
- Trong OnError, hàm nên được truyền phải chứa ba tham số. path name passed which raised the exception while removal
- Chức năng - Chức năng nâng cao ngoại lệ. exception info raised by sys.exc_info[]
Đường dẫn - tên đường dẫn được truyền đi trong khi loại bỏ
Python3
import
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed08
import
os
location
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed13
import
5
Deleted 'Untitled Folder' successfully2
Deleted 'Untitled Folder' successfully3
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed51
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6
import
5
Deleted 'Untitled Folder' successfully2
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed55
location
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed13
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed0
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed16
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed2
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed0
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed6
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed67
=
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed69
Output:
Ví dụ 2: Bỏ qua lỗi trong khi xóa thư mục
Bằng cách vượt qua bỏ qua_errors = true. & Nbsp;
Traceback [cuộc gọi gần đây nhất cuối cùng]: File \ Chương trình \ Python \ Python38-32 \ lib \ shatil.py, dòng 730, trong rmtree return _rmtree_unsafe [path, onerror] File lib \ swutil.py, dòng 589, trong _rmtree_unsafe onerror [os.scandir, path, sys.exc_info []] Shutil.py, dòng 586, trong _rmtree_unSafe với Os.Scandir [đường dẫn] dưới dạng scandir_it: filenotfounderror: [WinError 3] Hệ thống không thể tìm thấy đường dẫn được chỉ định: '
Ví dụ 3: Trình xử lý ngoại lệ
Trong OnError, hàm nên được truyền phải chứa ba tham số.
Parameter:
- Chức năng - Chức năng nâng cao ngoại lệ. A path-like object representing a empty directory path. A path-like object is either a string or bytes object representing a path.
Đường dẫn - tên đường dẫn được truyền đi trong khi loại bỏ This method does not return any value.
ExcInfo - Thông tin ngoại lệ được nêu bởi sys.exc_info []
Dưới đây là triển khai & NBSP;
Python3
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed46
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed47
Bên trong Handler [, FilenotFounderror [2, System Hệ thống không thể tìm thấy đường dẫn được chỉ định],] bên trong Handler [, FilenotFounderror [2, System Hệ thống không thể tìm thấy tệp được chỉ định],]
Xóa tệp/Dir
Một thư mục trống cũng có thể được xóa hoặc xóa bằng phương thức Module PathLib Module RMDIR []. Đầu tiên, chúng tôi phải & nbsp; đặt đường dẫn cho thư mục, và sau đó chúng tôi & nbsp; gọi phương thức rmdir [] trên đường dẫn đó
Output:
Deleted 'Untitled Folder' successfully