Hướng dẫn python delete file as root - python xóa tệp dưới dạng thư mục gốc

Tôi đã cố gắng sử dụng swutil.move () để di chuyển một bộ sưu tập các tệp riêng lẻ từ thư mục của chúng trong một ổ đĩa sang ổ khác và nó dường như thành công trong việc sao chép tệp đầu tiên, tuy nhiên khi xóa nó lỗi:shutil.move() to move a collection of files individually from their directory in one drive to another drive and it appears to succeed on copying the first file, however when it comes to delete the remove it runs into a permission error:

PermissionError: [WinError 5] Access is denied: 'C:\\Program Files\\openPDC\\Archive\\ppa_archive_2020-03-04 15!25!29.120_to_2020-03-04 20!16!30.900.d'

Tôi có thể xóa thủ công tệp bằng tay bằng cách sử dụng quản trị viên Privledge nhưng tôi không chắc chắn về cách gọi admin Privledge trong tập lệnh Python để cho phép showil.move () vào xóa tệp gốc. Tôi đã cố gắng loại bỏ quyền truy cập chỉ đọc nhưng điều đó dường như không giúp ích gì, bất kỳ ý tưởng nào?admin privledge but I am unsure on how to invoke admin privledge within the Python script to allow the shutil.move() to the remove the original file. I have tried to remove the read-only access but that doesn't seem to help, any ideas?

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise

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ụ: Xóa một thư mục trống bằng rmdir ()

Trong ví dụ này, chúng tôi sẽ xóa một thư mục trống, chúng tôi chỉ cần chỉ định tên thư mục nếu nó nằm trong thư mục gốc

Hướng dẫn python delete file as root - python xóa tệp dưới dạng thư mục gốc

import

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
73

Python3

import os

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
48
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
49

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
1=
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
15

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'
2 =
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
18

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
9

Output:  

Hướng dẫn python delete file as root - python xóa tệp dưới dạng thư mục gốc

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
4=
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
6
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'
222

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),)

Python3

import os

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
48
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
49

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
1=
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
15

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
4=
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
6
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'
222

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
9

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),)

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'

Xóa tệp/Dir Check if File Exists Before Deleting

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 đó

Python3

import os

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
48
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
49

[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
5
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
6

[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
7
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
9

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
1=
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
15

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'
2 =
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
18

[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
7
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
4
Deleted 'Untitled Folder' successfully
9

[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
7
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
4
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
5import3
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
8

Output:

[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
4=
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
6
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'
222
To know more about os.remove() click here.

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/DirOSError will be raised if the specified path is not an empty directory.

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 đó

Cú pháp của pathlib.pathos.rmdir(path, *, dir_fd = None) 

Parameter:  

  • Đường dẫn: Một đối tượng giống như đường dẫn đại diện cho một đườ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; 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 tất cả các thư mục khỏi thư mục Delete all directories from a Directory

Giả sử các thư mục là - & nbsp;

Hướng dẫn python delete file as root - python xóa tệp dưới dạng thư mục gốc

Chúng tôi muốn loại bỏ các chuyên viên viên thư mục. Dưới đây là việc thực hiện. & NBSP;

Python3

import os

import7= import9

os0= os2

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
4= os5

os6

Output:  

Hướng dẫn python delete file as root - python xóa tệp dưới dạng thư mục gốc

Ví dụ 2: Xử lý lỗi trong khi xóa thư mục

Xử lý lỗi trong khi sử dụng phương thức os.rmdir (), & nbsp;

Python3

import os

import7= import9

os0= os2

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
4= os5

[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
5
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
6

Ví dụ 2: Xử lý lỗi trong khi xóa thư mục

Xử lý lỗi trong khi sử dụng phương thức os.rmdir (), & nbsp;

import7= file1

[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
7
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
4
Deleted 'Untitled Folder' successfully
9

[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
7os6

Output:

[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed

[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
7
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
4
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
5=5
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
7 =7
To know more about os.rmdir() click here.

Deleted 'Untitled Folder' successfully5 Deleted 'Untitled Folder' successfully6

[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
7
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
4
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
5
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
06
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
7 =7

Lưu ý: Để biết thêm về OS.RMDIR () Bấm vào đây.

Xóa tệp/DIR bằng cách sử dụng Shutil.rmtree () shutil.rmtree(path, ignore_errors=False, onerror=None) 

Parameters:  

  • Đường dẫn: Một đối tượng giống như đường dẫn đại diện cho một đườ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;A path-like object representing a file path. A path-like object is either a string or bytes object representing a path. 
  • SOWN.RMTREE () được sử dụng để xóa toàn bộ cây thư mục, đường dẫn phải trỏ đến một thư mục (nhưng không phải là một liên kết tượng trưng đến một thư mục). If ignore_errors is true, errors resulting from failed removals will be ignored. 
  • Cú pháp của SOWL.RMTREE () If ignore_errors is false or omitted, such errors are handled by calling a handler specified by onerror.

Cú pháp: shutil.rmtree (đường dẫn, bỏ qua_errors = false, onerror = none) & nbsp;

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; 

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.

Xóa một thư mục và các tệp có trong đó.# Parent directory: 

Hướng dẫn python delete file as root - python xóa tệp dưới dạng thư mục gốc

Ví dụ 1: & nbsp;# Directory inside parent directory: 

Hướng dẫn python delete file as root - python xóa tệp dưới dạng thư mục gốc

Giả sử thư mục và các thư mục phụ như sau. 

Hướng dẫn python delete file as root - python xóa tệp dưới dạng thư mục gốc

& nbsp;# thư mục cha mẹ: & nbsp;

& nbsp;# thư mục bên trong thư mục mẹ: & nbsp;

Python3

# Tệp bên trong thư mục phụ: & nbsp;

import os

import7= import9

os0= os2

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
4= os5

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
24

Output:  

Hướng dẫn python delete file as root - python xóa tệp dưới dạng thư mục gốc

Ví dụ 2: Xử lý lỗi trong khi xóa thư mục

Xử lý lỗi trong khi sử dụng phương thức os.rmdir (), & nbsp;

Python3

# Tệp bên trong thư mục phụ: & nbsp;

import os

import7= import9

os0= os2

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
4= os5

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
40=
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
42
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
8

Output:

Ví dụ 2: Xử lý lỗi trong khi xóa thư mục

Xử lý lỗi trong khi sử dụng phương thức os.rmdir (), & nbsp;

import7= file1

  • [WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
    Directory 'GeeksforGeeks' can not be removed
    7os6
    function which raised the exception.
  • [WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
    Directory 'GeeksforGeeks' can not be removed
    7
    [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
    File path can not be removed
    4
    [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
    File path can not be removed
    5=5
    [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
    File path can not be removed
    7 =7
    path name passed which raised the exception while removal
  • Deleted 'Untitled Folder' successfully
    5
    Deleted 'Untitled Folder' successfully
    6
    exception info raised by sys.exc_info()

[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
7
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
4
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
5
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
06
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
7 =7

Python3

# Tệp bên trong thư mục phụ: & nbsp;

import os

import7= import9

[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
7
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
4
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
5
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
53
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
8

[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks'
Directory 'GeeksforGeeks' can not be removed
7
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
4
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
57

import7= import9

os0= os2

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
4= os5

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
69=
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
71

Output:

Ví dụ 2: Xử lý lỗi trong khi xóa thư mục

Xử lý lỗi trong khi sử dụng phương thức os.rmdir (), & nbsp;

import7= file1

Cú pháp của pathlib.path

Cú pháp: pathlib.path (trống_dir_path) .rmdir ()

Parameter:  

  • trống_dir_path: một đối tượng giống như đường dẫn đại diện cho một đường dẫn thư mục trống.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; A path-like object representing a empty directory path. A path-like object is either a string or bytes object representing a path. 

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ụ: Xóa một thư mục trống bằng rmdir ()

Trong ví dụ này, chúng tôi sẽ xóa một thư mục trống, chúng tôi chỉ cần chỉ định tên thư mục nếu nó nằm trong thư mục gốc

Python3

import

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
73

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
74=
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
76
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
77

            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
4=
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
80

[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
4
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
5
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
83
[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil'
File path can not be removed
7
            try:
                os.chmod(files[i], stat.S_IWRITE )
                os.unlink(files[i])
                shutil.move(files[i], destination)
            except PermissionError:
                print ("Error: Could not move file {} from {} to {}".format(files[i], source, destination))
                raise
85

Output:

Deleted 'Untitled Folder' successfully