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: Show
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? 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à -
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 ()
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
73Python3
48 49 1= 15Traceback (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 = 18 9Output:
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
48 49 1= 15 4= 6Traceback (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 9Bê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
48 49[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed5 [WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed6 [WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed7 9 1= 15Traceback (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 = 18[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed7 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4 Deleted 'Untitled Folder' successfully9 [WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed7 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed5 import 3 8Output: [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed
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 đó
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; 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
4= os 5
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; Python3
4= os 5[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed5 [WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed6 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;
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed7 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4 Deleted 'Untitled Folder' successfully9 [WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed7 os 6Output: [WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed
Deleted 'Untitled Folder' successfully5 Deleted 'Untitled Folder' successfully6[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed7 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed5 06 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed7 = 7Lưu ý: Để biết thêm về OS.RMDIR () Bấm vào đây.
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: Ví dụ 1: & nbsp;# Directory inside parent directory: Giả sử thư mục và các thư mục phụ như sau. & 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;
4= os 5 24Output: 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;
4= os 5 40= 42 8Output:
Xử lý lỗi trong khi sử dụng phương thức os.rmdir (), & nbsp;
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed7 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed5 06 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed7 = 7Python3# Tệp bên trong thư mục phụ: & nbsp;
[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed7 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed5 53 8[WinError 145] The directory is not empty: 'D:/Pycharm projects/GeeksforGeeks' Directory 'GeeksforGeeks' can not be removed7 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4 57
4= os 5 69= 71Output:
Xử lý lỗi trong khi sử dụng phương thức os.rmdir (), & nbsp;
Cú pháp của pathlib.path
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
73 74= 76 77 4= 80[Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed4 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed5 83 [Errno 21] Is a directory: 'D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil' File path can not be removed7 85Output: Deleted 'Untitled Folder' successfully |