Python xóa thư mục không trống

Để 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ệp

Mô-đ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ĩa

import 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]
23

Xó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]
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 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ượ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]
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ư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ẫ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ày

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 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]
4

Mô-đ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]
34

import os

file_path = '/tmp/file.txt'
os.unlink[file_path]
2

Xó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ống

Ví 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]
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ố thao tác cấp cao trên tệp và thư mục

Vớ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ục

Sự 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

Chức năng xóa một thư mục không trống là gì?

Thư mục không rỗng nghĩa là thư mục có tệp hoặc thư mục con. Chúng ta có thể xóa thư mục bằng cách sử dụng phương thức Delete[] của lớp Directory.

Công dụng của Rmtree[] trong Python là gì?

rmtree[] được sử dụng để xóa toàn bộ cây thư mục , đường dẫn phải trỏ tới một thư mục [chứ không phải là liên kết tượng trưng tới thư mục . Thông số. đường dẫn. Một đối tượng giống như đường dẫn đại diện cho đườ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.

Chủ Đề