Hướng dẫn này giải thích cách nào để xóa các tập tin hoặc thư mục bằng chức năng 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
Nội dung chính
- 1 Xóa tệp
- 1. 1 Khớp mẫu
- 2 Xóa thư mục [thư mục]
- 3 Phần kết luận
- 3. 1 Chia sẻ cái này.
- 3. 2 Thích cái này.
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ập tin duy nhất
mô-đun
import os file_path = '/tmp/file.txt' os.unlink[file_path]0 cung cấp các cách để tương tác với hệ điều hành. Mô-đun có sẵn cho cả Python 2 và 3
Để xóa một tệp duy nhất bằng
import os file_path = '/tmp/file.txt' os.unlink[file_path]3, hãy chuyển đường dẫn đến tệp dưới dạng đối số
import os file_path = '/tmp/file.txt' os.unlink[file_path]3và
import os file_path = '/tmp/file.txt' os.unlink[file_path]4 các chức năng giống nhau về mặt ngữ nghĩa
import os file_path = '/tmp/file.txt' os.unlink[file_path]
If tep chỉ được định không tồn tại, lỗi
import os file_path = '/tmp/file.txt' os.unlink[file_path]20 sẽ được đưa ra. Cả hai
import os file_path = '/tmp/file.txt' os.unlink[file_path]3và chỉ
import os file_path = '/tmp/file.txt' os.unlink[file_path]4có thể xóa các tệp, không phải thư mục. Nếu đường dẫn đã dẫn đến một thư mục, chúng tôi sẽ phát hiện lỗi
import os file_path = '/tmp/file.txt' os.unlink[file_path]23
Xóa tệp yêu cầu ghi quyền và thực thi tệp 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]2
Mô-đ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 các đối tượng giao diện hướ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 tệp với mô-đun
import os file_path = '/tmp/file.txt' os.unlink[file_path]1, hãy tạo đối tượng con trỏ
import os file_path = '/tmp/file.txt' os.unlink[file_path]28 đế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ượng đó
import os file_path = '/tmp/file.txt' os.unlink[file_path]3
import os file_path = '/tmp/file.txt' os.unlink[file_path]5,
import os file_path = '/tmp/file.txt' os.unlink[file_path]3và
import os file_path = '/tmp/file.txt' os.unlink[file_path]4 cũng có thể được sử dụng để xoá một liên kết biểu tượng [liên kết tượng trưng]
khớ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ột 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ư sau
import 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 vào hàm
import os file_path = '/tmp/file.txt' os.unlink[file_path]38và sử dụng mẫu “ ** `
import os file_path = '/tmp/file.txt' os.unlink[file_path]4
Mô-đun
import os file_path = '/tmp/file.txt' os.unlink[file_path]1 bao gồm hai chức năng toàn cầu,
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 để phù hợp với tệp trong một thư mục định sẵn.
import os file_path = '/tmp/file.txt' os.unlink[file_path]38chỉ 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]91khớp với tất cả các tệp trong thư mục và tất cả các thư mục con, một cách đệ quy. Ví dụ mã sau sẽ xóa tất cả các tệp ___233 trong thư mục
import os file_path = '/tmp/file.txt' os.unlink[file_path]34
import os file_path = '/tmp/file.txt' os.unlink[file_path]2
Delete the directory [thư mục]
Trong Python, bạn có thể sử dụng
import os file_path = '/tmp/file.txt' os.unlink[file_path]96và
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 trống
Ví dụ sau đây để thấy cách xóa một thư mục trống
import os file_path = '/tmp/file.txt' os.unlink[file_path]6
Ngoà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]1
import os file_path = '/tmp/file.txt' os.unlink[file_path]8
Mô-đ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ố hoạt động cấp cao trên các tập tin và thư mục
Với
import os file_path = '/tmp/file.txt' os.unlink[file_path]98chức năng này, bạn có thể xóa một thư mục định bao gồm nội dung của nó
import os file_path = '/tmp/file.txt' os.unlink[file_path]1
Đối số được truyền tới
import os file_path = '/tmp/file.txt' os.unlink[file_path]98không thể là một biểu tượng liên kết [liên kết tượng trưng] đến một thư mục
Phần kết luận
Python cung cấp một số mô-đun để xử lý tệp
Chúng ta đã tìm ra cách nào để 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 tin duy nhất,
import os file_path = '/tmp/file.txt' os.unlink[file_path]96và
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đệ quy định xóa một thư mục và tất cả cá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, nó không thể dễ dàng khôi phục lại