Python tồn tại phương thức [] được sử dụng để kiểm tra xem tệp hoặc thư mục cụ thể có tồn tại hay không. Nó cũng được sử dụng để kiểm tra xem một đường dẫn có đề cập đến bất kỳ mô tả tệp mở nào hay không. Nó trả về giá trị boolean true nếu tệp tồn tại và trả về sai. Nó được sử dụng với mô -đun HĐH và mô -đun phụ OS.Path làm OS.Path.exists [đường dẫn]. method is used to check whether specific file or directory exists or not. It is also used to check if a path refers to any open file descriptor or not. It returns boolean value true if file exists and returns false otherwise. It is used with os module and os.path sub module as os.path.exists[path]. Trong tệp Python này tồn tại hướng dẫn, chúng ta sẽ tìm hiểu cách xác định xem một tệp [hoặc thư mục] có tồn tại bằng Python hay không. Để kiểm tra xem tệp có tồn tại Python hay không, chúng tôi sử dụng kiểm tra python thư viện tích hợp nếu tệp có tồn tại chức năng không. Có nhiều cách khác nhau để xác minh tệp hoặc kiểm tra Python nếu thư mục tồn tại, sử dụng các chức năng như được liệt kê dưới đây. Sử dụng Path.Exists Bạn có thể nhanh chóng kiểm tra xem tệp hoặc thư mục có tồn tại không. Dưới đây là các bước để kiểm tra Python có tồn tại hay không: Bước 1] Nhập mô -đun OS.Path Trước khi bạn chạy mã, điều quan trọng là bạn phải nhập mô -đun OS.Path. Bước 2] Sử dụng Path.Exists [] Funtion Bây giờ, sử dụng hàm path.exists [] để kiểm tra python nếu một tệp tồn tại. Bước 3] Chạy mã được đưa ra dưới đây Đây là mã hoàn chỉnh Trong trường hợp của chúng tôi, chỉ có tệp Guru99.txt được tạo trong thư mục làm việc Output: Tệp tồn tại: Tệp true tồn tại: Thư mục sai tồn tại: Sai Phương thức python isfile [] được sử dụng để tìm xem một đường dẫn nhất định có phải là một tệp thông thường hiện có hay không. Nó trả về một giá trị boolean true nếu đường dẫn cụ thể là một tệp hiện có hoặc nếu không nó sẽ trả về sai. Nó có thể được sử dụng bởi cú pháp: Os.Path.isfile [đường dẫn].Python isfile[] method is used to find whether a given path is an existing regular file or not. It
returns a boolean value true if the specific path is an existing file or else it returns false. It can be used by the syntax : os.path.isfile[path]. Chúng ta có thể sử dụng lệnh isfile để kiểm tra xem một đầu vào đã cho có phải là tệp hay không. Output: Nó có phải là tập tin không? Đúng là tập tin? Sai Nếu chúng tôi muốn xác nhận rằng một đường dẫn đã cho đến một thư mục, chúng tôi có thể sử dụng hàm os.path.dir [] Output: Nó có phải là thư mục không? Sai là thư mục? ĐÚNG VẬY Các phiên bản Python 3.4 trở lên có mô -đun Pathlib để xử lý với đường dẫn hệ thống tệp. Nó sử dụng cách tiếp cận hướng đối tượng để kiểm tra Python nếu thư mục có tồn tại hay không. Output: Tập tin tồn tại Hoàn thành mã Đây là mã hoàn chỉnh Output: Trong trường hợp của chúng tôi, chỉ có tệp Guru99.txt được tạo trong thư mục làm việcCách kiểm tra xem một tệp có tồn tại trong Python bằng Os.Path.exists []
import os.path
from os import path
path.exists["guru99.txt"]
import os.path
from os import path
def main[]:
print ["File exists:"+str[path.exists['guru99.txt']]]
print ["File exists:" + str[path.exists['career.guru99.txt']]]
print ["directory exists:" + str[path.exists['myDirectory']]]
if __name__== "__main__":
main[]
File exists: False
directory exists: FalsePython isfile []
os.path.isfile[]
import os.path
from os import path
def main[]:
print ["Is it File?" + str[path.isfile['guru99.txt']]]
print ["Is it File?" + str[path.isfile['myDirectory']]]
if __name__== "__main__":
main[]
Is it File? Falseos.path.isdir[]
import os.path
from os import path
def main[]:
print ["Is it Directory?" + str[path.isdir['guru99.txt']]]
print ["Is it Directory?" + str[path.isdir['myDirectory']]]
if __name__== "__main__":
main[]
Is it Directory? Truepathlibpath.exists [] cho Python 3.4
import pathlib
file = pathlib.Path["guru99.txt"]
if file.exists []:
print ["File exist"]
else:
print ["File not exist"]
import os
from os import path
def main[]:
# Print the name of the OS
print[os.name]
#Check for item existence and type
print["Item exists:" + str[path.exists["guru99.txt"]]]
print["Item is a file: " + str[path.isfile["guru99.txt"]]]
print["Item is a directory: " + str[path.isdir["guru99.txt"]]]
if __name__ == "__main__":
main[]
Item is a file: True
Item is a directory: FalseTệp tồn tại: Tệp true tồn tại: Thư mục sai tồn tại: Sai
Nếu chúng tôi muốn xác nhận rằng một đường dẫn đã cho đến một thư mục, chúng tôi có thể sử dụng hàm os.path.dir []Python Tutorial for Beginners: Learn Programming Basics [PDF]
Tóm tắt: Trong hướng dẫn này, bạn sẽ học cách kiểm tra xem một tệp có tồn tại không.: in this tutorial, you’ll learn how to check if a file exists.
Khi xử lý các tệp, bạn sẽ thường muốn kiểm tra xem một tệp có tồn tại trước khi làm việc khác với nó như đọc từ tệp hoặc ghi vào nó không.
Để thực hiện, bạn có thể sử dụng hàm
import os.path from os import path def main[]: print ["File exists:"+str[path.exists['guru99.txt']]] print ["File exists:" + str[path.exists['career.guru99.txt']]] print ["directory exists:" + str[path.exists['myDirectory']]] if __name__== "__main__": main[]9 từ phương thức
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]0 hoặc phương thức
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]1 từ lớp
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]2 trong mô -đun
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]3.
hàm os.path.exists []
Code language: JavaScript [javascript]
from os.path import exists file_exists = exists[path_to_file]
Phương thức path.is_file []
Code language: JavaScript [javascript]
from pathlib import Path path = Path[path_to_file] path.is_file[]
1] Sử dụng hàm os.path.exists [] để kiểm tra xem tệp có tồn tại
Để kiểm tra xem tệp có tồn tại không, bạn chuyển đường dẫn tệp đến hàm
import os.path from os import path def main[]: print ["File exists:"+str[path.exists['guru99.txt']]] print ["File exists:" + str[path.exists['career.guru99.txt']]] print ["directory exists:" + str[path.exists['myDirectory']]] if __name__== "__main__": main[]9 từ thư viện tiêu chuẩn
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]0.
Đầu tiên, nhập thư viện tiêu chuẩn
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]0:
Code language: JavaScript [javascript]
import os.path
Thứ hai, hãy gọi hàm
import os.path from os import path def main[]: print ["File exists:"+str[path.exists['guru99.txt']]] print ["File exists:" + str[path.exists['career.guru99.txt']]] print ["directory exists:" + str[path.exists['myDirectory']]] if __name__== "__main__": main[]9:
path.exists["guru99.txt"]0
Nếu tệp tồn tại, hàm
import os.path from os import path def main[]: print ["File exists:"+str[path.exists['guru99.txt']]] print ["File exists:" + str[path.exists['career.guru99.txt']]] print ["directory exists:" + str[path.exists['myDirectory']]] if __name__== "__main__": main[]9 trả về
import os.path from os import path def main[]: print ["File exists:"+str[path.exists['guru99.txt']]] print ["File exists:" + str[path.exists['career.guru99.txt']]] print ["directory exists:" + str[path.exists['myDirectory']]] if __name__== "__main__": main[]2. Nếu không, nó trả về
import os.path from os import path def main[]: print ["Is it Directory?" + str[path.isdir['guru99.txt']]] print ["Is it Directory?" + str[path.isdir['myDirectory']]] if __name__== "__main__": main[]0.
Nếu tệp nằm trong cùng một thư mục với chương trình,
import os.path from os import path def main[]: print ["Is it Directory?" + str[path.isdir['guru99.txt']]] print ["Is it Directory?" + str[path.isdir['myDirectory']]] if __name__== "__main__": main[]1 chỉ đơn giản là tên tệp.
Tuy nhiên, nó không phải là trường hợp, bạn cần vượt qua đường dẫn tệp đầy đủ của tệp. Ví dụ:
path.exists["guru99.txt"]1
Ngay cả khi bạn chạy chương trình trên Windows, bạn nên sử dụng Slash phía trước [
import os.path from os import path def main[]: print ["Is it Directory?" + str[path.isdir['guru99.txt']]] print ["Is it Directory?" + str[path.isdir['myDirectory']]] if __name__== "__main__": main[]2] để tách đường dẫn. Nó sẽ hoạt động trên các Windows, MacOS và Linux.
Ví dụ sau sử dụng chức năng
import os.path from os import path def main[]: print ["File exists:"+str[path.exists['guru99.txt']]] print ["File exists:" + str[path.exists['career.guru99.txt']]] print ["directory exists:" + str[path.exists['myDirectory']]] if __name__== "__main__": main[]9 để kiểm tra xem tệp
import os.path from os import path def main[]: print ["Is it Directory?" + str[path.isdir['guru99.txt']]] print ["Is it Directory?" + str[path.isdir['myDirectory']]] if __name__== "__main__": main[]4 có tồn tại trong cùng thư mục với chương trình không:
path.exists["guru99.txt"]2
Nếu tệp
import os.path from os import path def main[]: print ["Is it Directory?" + str[path.isdir['guru99.txt']]] print ["Is it Directory?" + str[path.isdir['myDirectory']]] if __name__== "__main__": main[]4 tồn tại, bạn sẽ thấy đầu ra sau:
path.exists["guru99.txt"]3
Nếu không, bạn sẽ thấy
import os.path from os import path def main[]: print ["Is it Directory?" + str[path.isdir['guru99.txt']]] print ["Is it Directory?" + str[path.isdir['myDirectory']]] if __name__== "__main__": main[]0 trên màn hình:
path.exists["guru99.txt"]4
Để thực hiện cuộc gọi đến chức năng
import os.path from os import path def main[]: print ["File exists:"+str[path.exists['guru99.txt']]] print ["File exists:" + str[path.exists['career.guru99.txt']]] print ["directory exists:" + str[path.exists['myDirectory']]] if __name__== "__main__": main[]9 ngắn hơn và rõ ràng hơn, bạn có thể nhập chức năng đó và đổi tên thành hàm
import os.path from os import path def main[]: print ["Is it Directory?" + str[path.isdir['guru99.txt']]] print ["Is it Directory?" + str[path.isdir['myDirectory']]] if __name__== "__main__": main[]8 như thế này:
path.exists["guru99.txt"]5
2] Sử dụng mô -đun Pathlib để kiểm tra xem tệp có tồn tại không
Python đã giới thiệu mô -đun
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]3 kể từ phiên bản 3.4.
Mô-đun
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]3 cho phép bạn thao tác các tệp và thư mục bằng cách sử dụng phương pháp hướng đối tượng. Nếu bạn không quen thuộc với lập trình hướng đối tượng, hãy xem phần Python OOP.
Đầu tiên, nhập lớp
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]2 từ mô -đun
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]3:
path.exists["guru99.txt"]6
Sau đó, khởi tạo một thể hiện mới của lớp
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]2 và khởi tạo nó với đường dẫn tệp mà bạn muốn kiểm tra sự tồn tại:
path.exists["guru99.txt"]7
Cuối cùng, hãy kiểm tra xem tệp có tồn tại bằng phương pháp
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]1 không:
path.exists["guru99.txt"]8
Nếu tệp không tồn tại, phương thức
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]1 trả về
import os.path from os import path def main[]: print ["Is it Directory?" + str[path.isdir['guru99.txt']]] print ["Is it Directory?" + str[path.isdir['myDirectory']]] if __name__== "__main__": main[]0. Nếu không, nó trả về
import os.path from os import path def main[]: print ["File exists:"+str[path.exists['guru99.txt']]] print ["File exists:" + str[path.exists['career.guru99.txt']]] print ["directory exists:" + str[path.exists['myDirectory']]] if __name__== "__main__": main[]2.
Ví dụ sau đây cho thấy cách sử dụng lớp
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]2 từ mô -đun
import os.path from os import path def main[]: print ["Is it File?" + str[path.isfile['guru99.txt']]] print ["Is it File?" + str[path.isfile['myDirectory']]] if __name__== "__main__": main[]3 để kiểm tra xem tệp
import os.path from os import path def main[]: print ["Is it Directory?" + str[path.isdir['guru99.txt']]] print ["Is it Directory?" + str[path.isdir['myDirectory']]] if __name__== "__main__": main[]4 có tồn tại trong cùng một thư mục của chương trình không:
path.exists["guru99.txt"]9
Nếu tệp
import os.path from os import path def main[]: print ["Is it Directory?" + str[path.isdir['guru99.txt']]] print ["Is it Directory?" + str[path.isdir['myDirectory']]] if __name__== "__main__": main[]4 tồn tại, bạn sẽ thấy đầu ra sau:
import os.path from os import path def main[]: print ["File exists:"+str[path.exists['guru99.txt']]] print ["File exists:" + str[path.exists['career.guru99.txt']]] print ["directory exists:" + str[path.exists['myDirectory']]] if __name__== "__main__": main[]0
Bản tóm tắt
- Sử dụng chức năng
import os.path from os import path def main[]: print ["File exists:"+str[path.exists['guru99.txt']]] print ["File exists:" + str[path.exists['career.guru99.txt']]] print ["directory exists:" + str[path.exists['myDirectory']]] if __name__== "__main__": main[]
1 hoặc phương thứcimport os from os import path def main[]: # Print the name of the OS print[os.name] #Check for item existence and type print["Item exists:" + str[path.exists["guru99.txt"]]] print["Item is a file: " + str[path.isfile["guru99.txt"]]] print["Item is a directory: " + str[path.isdir["guru99.txt"]]] if __name__ == "__main__": main[]
3 để kiểm tra xem tệp có tồn tại không
Bạn có thấy hướng dẫn này hữu ích không?