Để xóa/xóa bất kỳ thư mục nào không trống bằng Python, chúng tôi có một thư viện chuẩn được gọi là Shutil. Thư viện này cung cấp chức năng shutdown. rmtree[] loại bỏ/xóa thư mục không trống
đóng cửa. rmtree[]. -
Chức năng này xóa toàn bộ cây thư mục;
Hướng dẫn này giải thích cách xóa tệp và thư mục bằng cách sử dụng các hàm từ mô-đun
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
0, import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
1 và import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
2Đang xóa tệp #
Trong Python, bạn có thể sử dụng
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
3, import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
4, import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
5 để xóa một tệpMô-đun
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
0 cung cấp cách tương tác di động với hệ điều hành. Mô-đun có sẵn cho cả Python 2 và 3Để xóa một tệp có
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
3, hãy chuyển đường dẫn đến tệp làm đối số________số 8
Các hàm
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
3 và import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
4 giống hệt nhau về mặt ngữ nghĩaimport os
file_path = '/tmp/file.txt'
os.unlink[file_path]
Nếu tệp được chỉ định không tồn tại, lỗi
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
20 sẽ xuất hiện. Cả import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
3 và import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
4 chỉ có thể xóa tệp, không xóa thư mục. Nếu đường dẫn đã cho trỏ đến một thư mục, chúng sẽ báo lỗi import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
23Xóa tệp cần có quyền ghi và thực thi trên thư mục chứa tệp. Nếu không, bạn sẽ gặp lỗi
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
24Để tránh lỗi khi xóa tệp, bạn có thể sử dụng xử lý ngoại lệ để bắt ngoại lệ và gửi thông báo lỗi thích hợp
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
2Mô-đun
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
1 có sẵn trong Python 3. 4 trở lên. Nếu bạn muốn sử dụng mô-đun này trong Python 2, bạn có thể cài đặt nó bằng pip. import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
1 cung cấp giao diện hướng đối tượng để làm việc với các đường dẫn hệ thống tệp cho các hệ điều hành khác nhauĐể xóa một tệp với mô-đun
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
1, hãy tạo một đối tượng import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
28 trỏ đến tệp và gọi phương thức import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
29 trên đối tượngimport os
file_path = '/tmp/file.txt'
os.unlink[file_path]
3import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
5, import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
3 và import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
4 cũng có thể được sử dụng để xóa một liên kết tượng trưngKhớp mẫu #
Bạn có thể sử dụng mô-đun toàn cầu để khớp nhiều tệp dựa trên mẫu. Ví dụ: để xóa tất cả các tệp
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
33 trong thư mục import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
34, bạn sẽ sử dụng một cái gì đó như thế nàyimport os
file_path = '/tmp/file.txt'
os.unlink[file_path]
9Để xóa đệ quy tất cả các tệp
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
33 trong thư mục import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
34 và tất cả các thư mục con bên dưới nó, hãy chuyển đối số import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
37 cho hàm import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
38 và sử dụng mẫu ``**`import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
4Mô-đun
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
1 bao gồm hai hàm tổng thể, import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
38 và import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
91 để khớp các tệp trong một thư mục nhất định. import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
38 chỉ khớp với các tệp trong thư mục cấp cao nhất. import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
91 khớp với tất cả các tệp trong thư mục và tất cả các thư mục con, theo cách đệ quy. Mã ví dụ sau xóa tất cả các tệp import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
33 trong thư mục import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
34import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
2Xóa thư mục [Folders] #
Trong Python, bạn có thể sử dụng
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
96 và import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
97 để xóa một thư mục trống và import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
98 để xóa một thư mục không trốngVí dụ sau đây cho thấy cách xóa một thư mục trống
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
6Ngoài ra, bạn có thể xóa các thư mục bằng mô-đun
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
1import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
8Mô-đun
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
2 cho phép bạn thực hiện một số thao tác cấp cao trên tệp và thư mụcVới chức năng
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
98, bạn có thể xóa một thư mục nhất định bao gồm cả nội dung của nóimport os
file_path = '/tmp/file.txt'
os.unlink[file_path]
1Đối số được chuyển đến
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
98 không thể là một liên kết tượng trưng đến một thư mụcSự kết luận #
Python cung cấp một số mô-đun để xử lý tệp
Chúng tôi đã chỉ cho bạn cách sử dụng
import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
3, import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
4, import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
5 để xóa một tệp, import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
96 và import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
97 để xóa một thư mục trống và import os
file_path = '/tmp/file.txt'
os.unlink[file_path]
98 để xóa đệ quy một thư mục và tất cả nội dung của nóHãy hết sức cẩn thận khi xóa tệp hoặc thư mục, vì một khi tệp đã bị xóa thì không thể khôi phục dễ dàng