Kiểm tra xem một thư mục tồn tại python và tạo

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

  • 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
    
    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

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

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

Việc sử dụng các phương thức mkdir[] và Mkdirs[] trong Python là gì?

mkdir[] và os. mkdirs[] có thể được sử dụng để tạo thư mục trong Python.

Làm cách nào để tạo một thư mục trong Python?

Tạo thư mục mới bằng Python . Phương thức này lấy đường dẫn của thư mục mới. Nếu đường dẫn đầy đủ không được chỉ định, thư mục mới sẽ được tạo trong thư mục làm việc hiện tại. using the mkdir[] method. This method takes in the path of the new directory. If the full path is not specified, the new directory is created in the current working directory.

Chủ Đề