Python ตรวจสอบว่าไฟล์มีอยู่หรือไม่ (พร้อมตัวอย่าง)
⚡ สรุปอย่างชาญฉลาด
Python ช่วยให้คุณตรวจสอบว่าไฟล์หรือไดเร็กทอรีมีอยู่หรือไม่โดยใช้โมดูล os.path และ pathlib ที่นี่คุณจะได้เรียนรู้ os.path.exists(), os.path.isfile(), os.path.isdir() และ pathlib.Path.exists() พร้อมตัวอย่างโค้ดที่ชัดเจนในแต่ละฟังก์ชัน

Python มีอยู่()
Python มีอยู่() วิธีนี้ใช้เพื่อตรวจสอบว่ามีไฟล์หรือไดเร็กทอรีที่ระบุอยู่หรือไม่ นอกจากนี้ยังใช้เพื่อตรวจสอบด้วยว่าเส้นทางอ้างอิงถึงตัวระบุไฟล์ที่เปิดอยู่หรือไม่ โดยจะส่งคืนค่าบูลีนเป็น true หากไฟล์มีอยู่ และส่งคืนค่า false หากไม่เป็นเช่นนั้น วิธีนี้ใช้กับโมดูล os และโมดูลย่อย os.path ในรูปแบบ os.path.exists(path)
ในการนี้ Python มีไฟล์อยู่แล้ว กวดวิชาเราจะเรียนรู้วิธีการตรวจสอบว่ามีไฟล์ (หรือไดเร็กทอรี) อยู่หรือไม่ Python- เพื่อตรวจสอบว่ามีไฟล์อยู่หรือไม่ Pythonเราใช้ไลบรารีในตัว Python ตรวจสอบว่ามีไฟล์อยู่หรือไม่
มีหลายวิธีในการตรวจสอบไฟล์หรือ Python ตรวจสอบว่ามีไดเร็กทอรีอยู่หรือไม่ โดยใช้ฟังก์ชันตามรายการด้านล่าง
วิธีตรวจสอบว่ามีไฟล์อยู่ในนั้นหรือไม่ Python ใช้ os.path.exists()
การใช้ path.exists ช่วยให้คุณสามารถตรวจสอบได้อย่างรวดเร็วว่ามีไฟล์หรือไดเร็กทอรีอยู่หรือไม่ นี่คือขั้นตอนสำหรับ Python ตรวจสอบไฟล์ที่มีอยู่หรือไม่:
ขั้นตอนที่ 1) นำเข้าโมดูล os.path
ก่อนที่คุณจะรันโค้ด สิ่งสำคัญคือคุณต้องนำเข้าโมดูล os.path
import os.path from os import path
ขั้นตอนที่ 2) ใช้ฟังก์ชัน path.exists()
ตอนนี้ใช้ฟังก์ชัน path.exists() เพื่อ Python ตรวจสอบว่ามีไฟล์อยู่หรือไม่
path.exists("guru99.txt")
ขั้นตอนที่ 3) เรียกใช้โค้ดที่ระบุด้านล่าง
นี่คือรหัสที่สมบูรณ์
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()
ในกรณีของเรา มีเพียงไฟล์ guru99.txt เท่านั้นที่ถูกสร้างขึ้นในไดเร็กทอรีการทำงาน
Output:
File exists: True File exists: False directory exists: False
Python ไอไฟล์()
การขอ Python ไอไฟล์() วิธีการใช้ในการค้นหาว่าเส้นทางที่กำหนดเป็นไฟล์ปกติที่มีอยู่หรือไม่ จะส่งกลับค่าบูลีนจริงหากเส้นทางเฉพาะเป็นไฟล์ที่มีอยู่ มิฉะนั้นจะส่งกลับค่าเท็จ สามารถใช้โดยไวยากรณ์: os.path.isfile(path)
os.path.isfile()
เราสามารถใช้คำสั่ง 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()
Output:
Is it File? True Is it File? False
os.path.isdir()
หากเราต้องการยืนยันว่าเส้นทางที่กำหนดชี้ไปยังไดเร็กทอรี เราสามารถใช้ฟังก์ชัน os.path.dir() ได้
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()
Output:
Is it Directory? False Is it Directory? True
pathlibPath.exists() สำหรับ Python 3.4
Python เวอร์ชัน 3.4 และสูงกว่ามีโมดูล pathlib สำหรับจัดการกับเส้นทางระบบไฟล์ มันใช้วิธีการเชิงวัตถุเพื่อ Python ตรวจสอบว่ามีโฟลเดอร์อยู่หรือไม่
import pathlib file = pathlib.Path("guru99.txt") if file.exists (): print ("File exist") else: print ("File not exist")
Output:
File exist
สมบูรณ์ Code
นี่คือรหัสที่สมบูรณ์
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()
Output:
Item exists: True Item is a file: True Item is a directory: False
