Khi bạn muốn mở một tệp và tệp hoặc thư mục tương ứng của đường dẫn đã cho không tồn tại, Python sẽ đưa ra một Ngoại lệ. Bạn nên giải quyết vấn đề này, nếu không mã của bạn sẽ bị sập
Bài viết này trình bày các cách khác nhau để kiểm tra xem tệp hoặc thư mục có tồn tại trong Python hay không và cách mở tệp một cách an toàn
Sử dụng khối try-except
Trước hết, thay vì kiểm tra xem tệp có tồn tại hay không, bạn hoàn toàn có thể trực tiếp mở tệp và bọc mọi thứ trong một khối try-except
. Chiến lược này còn được gọi là EAFP [Dễ xin phép hơn xin phép] và là một phong cách mã hóa Python được chấp nhận hoàn hảo
try:
f = open["filename.txt"]
except FileNotFoundError:
# doesn’t exist
else:
# exists
#hơn
Ghi chú. Trong Python 2, đây là một IOError
Sử dụng os.path.isfile[]
, os.path.isdir[]
hoặc os.path.exists[]
Nếu bạn không muốn tăng Ngoại lệ hoặc thậm chí bạn không cần mở tệp và chỉ cần kiểm tra xem nó có tồn tại hay không, bạn có các tùy chọn khác nhau. Cách đầu tiên là sử dụng các phương pháp khác nhau trong os.path
0. trả về True nếu đường dẫn là tệp hợp lệimport os if os.path.isfile["filename.txt"]: # file exists f = open["filename.txt"] if os.path.isdir["data"]: # directory exists if os.path.exists[file_path]: # file or directory exists
1. trả về True nếu đường dẫn là một thư mục hợp lệimport os if os.path.isfile["filename.txt"]: # file exists f = open["filename.txt"] if os.path.isdir["data"]: # directory exists if os.path.exists[file_path]: # file or directory exists
2. trả về True nếu đường dẫn là một tệp hoặc thư mục hợp lệimport os if os.path.isfile["filename.txt"]: # file exists f = open["filename.txt"] if os.path.isdir["data"]: # directory exists if os.path.exists[file_path]: # file or directory exists
import os
if os.path.isfile["filename.txt"]:
# file exists
f = open["filename.txt"]
if os.path.isdir["data"]:
# directory exists
if os.path.exists[file_path]:
# file or directory exists
Sử dụng mô-đun import os
if os.path.isfile["filename.txt"]:
# file exists
f = open["filename.txt"]
if os.path.isdir["data"]:
# directory exists
if os.path.exists[file_path]:
# file or directory exists
3 từ import os
if os.path.isfile["filename.txt"]:
# file exists
f = open["filename.txt"]
if os.path.isdir["data"]:
# directory exists
if os.path.exists[file_path]:
# file or directory exists
4
import os
if os.path.isfile["filename.txt"]:
# file exists
f = open["filename.txt"]
if os.path.isdir["data"]:
# directory exists
if os.path.exists[file_path]:
# file or directory exists
import os
if os.path.isfile["filename.txt"]:
# file exists
f = open["filename.txt"]
if os.path.isdir["data"]:
# directory exists
if os.path.exists[file_path]:
# file or directory exists
Bắt đầu với Python 3. 4, bạn có thể sử dụng mô-đun pathlib. Nó cung cấp một cách tiếp cận hướng đối tượng để làm việc với các đường dẫn hệ thống tệp và đây hiện là cách xử lý tệp và thư mục ưa thích của tôi
Bạn có thể tạo một đối tượng
import os
if os.path.isfile["filename.txt"]:
# file exists
f = open["filename.txt"]
if os.path.isdir["data"]:
# directory exists
if os.path.exists[file_path]:
# file or directory exists
5 như thế này________số 8_______
Bây giờ bạn có thể sử dụng các phương thức khác nhau
import os
if os.path.isfile["filename.txt"]:
# file exists
f = open["filename.txt"]
if os.path.isdir["data"]:
# directory exists
if os.path.exists[file_path]:
# file or directory exists
6, import os
if os.path.isfile["filename.txt"]:
# file exists
f = open["filename.txt"]
if os.path.isdir["data"]:
# directory exists
if os.path.exists[file_path]:
# file or directory exists
7 và import os
if os.path.isfile["filename.txt"]:
# file exists
f = open["filename.txt"]
if os.path.isdir["data"]:
# directory exists
if os.path.exists[file_path]:
# file or directory exists
8 trên đối tượng import os
if os.path.isfile["filename.txt"]:
# file exists
f = open["filename.txt"]
if os.path.isdir["data"]:
# directory exists
if os.path.exists[file_path]:
# file or directory exists
5