Trong Python, có nhiều cách khác nhau để kiểm tra xem một tệp có tồn tại hay không và xác định loại tệp
Hướng dẫn này chỉ ra ba kỹ thuật khác nhau về cách kiểm tra sự tồn tại của tệp
Kiểm tra xem tệp có tồn tại không
Cách đơn giản nhất để kiểm tra xem tệp có tồn tại hay không là thử mở tệp. Cách tiếp cận này không yêu cầu nhập bất kỳ mô-đun nào và hoạt động với cả Python 2 và 3. Sử dụng phương pháp này nếu bạn muốn mở tệp và thực hiện một số hành động
Đoạn mã sau đang sử dụng khối try-ngoại trừ đơn giản. Chúng tôi đang cố mở tệp
try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
2 và nếu tệp không tồn tại, một ngoại lệ try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
3 sẽ xuất hiện và thông báo "Không thể truy cập tệp" được intry:
f = open["filename.txt"]
# Do something with the file
except IOError:
print["File not accessible"]
finally:
f.close[]
Nếu bạn đang sử dụng Python 3, bạn cũng có thể sử dụng ngoại lệ
try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
4 thay vì try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
3Khi mở tệp, bạn nên sử dụng từ khóa
try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
6, từ khóa này đảm bảo tệp được đóng đúng cách sau khi hoàn tất các thao tác với tệp, ngay cả khi một ngoại lệ được đưa ra trong quá trình thao tác. Nó cũng làm cho mã của bạn ngắn hơn vì bạn không cần phải đóng tệp bằng hàm try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
0Đoạn mã sau tương đương với ví dụ trước
try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
Trong các ví dụ trên, chúng tôi đã sử dụng khối thử ngoại trừ và mở tệp để tránh tình trạng tương tranh. Điều kiện cạnh tranh xảy ra khi bạn có nhiều quy trình truy cập vào cùng một tệp
Ví dụ: khi bạn kiểm tra sự tồn tại của tệp, một quy trình khác có thể tạo, xóa hoặc chặn tệp trong khung thời gian giữa kiểm tra và mở tệp. Điều này có thể khiến mã của bạn bị hỏng
Kiểm tra xem tệp có tồn tại hay không bằng cách sử dụng hệ điều hành. đường dẫn Mô-đun
Mô-đun
try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
1 cung cấp một số chức năng hữu ích để làm việc với tên đường dẫn. Mô-đun có sẵn cho cả Python 2 và 3Trong ngữ cảnh của hướng dẫn này, các chức năng quan trọng nhất là
2 - Trả về true nếutry: with open['/etc/hosts'] as f: print[f.readlines[]] # Do something with the file except IOError: print["File not accessible"]
3 là tệp, thư mục hoặc liên kết tượng trưng hợp lệtry: with open['/etc/hosts'] as f: print[f.readlines[]] # Do something with the file except IOError: print["File not accessible"]
4 - Trả về true nếutry: with open['/etc/hosts'] as f: print[f.readlines[]] # Do something with the file except IOError: print["File not accessible"]
3 là tệp thông thường hoặc liên kết tượng trưng tới tệptry: with open['/etc/hosts'] as f: print[f.readlines[]] # Do something with the file except IOError: print["File not accessible"]
6 - Trả về true nếutry: with open['/etc/hosts'] as f: print[f.readlines[]] # Do something with the file except IOError: print["File not accessible"]
3 là một thư mục hoặc một liên kết tượng trưng tới một thư mụctry: with open['/etc/hosts'] as f: print[f.readlines[]] # Do something with the file except IOError: print["File not accessible"]
Câu lệnh
try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
8 sau đây kiểm tra xem tệp try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
2 có tồn tại khôngtry:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
1Sử dụng phương pháp này khi bạn cần kiểm tra xem tệp có tồn tại hay không trước khi thực hiện một hành động trên tệp. Ví dụ: sao chép hoặc xóa một tệp
Nếu bạn muốn mở và sửa đổi tệp, hãy sử dụng phương pháp trước đó
Kiểm tra xem Tệp có tồn tại hay không bằng cách sử dụng Mô-đun pathlib
Mô-đun
try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
10 có sẵn trong Python 3. 4 trở lên. Mô-đun này 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 nhauTương tự như ví dụ trước, đoạn mã sau kiểm tra xem tệp
try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
2 có tồn tại khôngtry:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
3try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
12 trả về true nếu try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
3 là tệp thông thường hoặc liên kết tượng trưng tới tệp. Để kiểm tra sự tồn tại của thư mục, hãy sử dụng phương thức try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
14Sự khác biệt chính giữa
try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
10 và try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
1 là try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
10 cho phép bạn làm việc với các đường dẫn dưới dạng các đối tượng try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
18 với các phương thức và thuộc tính có liên quan thay vì các đối tượng try:
with open['/etc/hosts'] as f:
print[f.readlines[]]
# Do something with the file
except IOError:
print["File not accessible"]
19 thông thường