Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีตรวจสอบว่าไดเรกทอรีมีอยู่จริงหรือไม่ใน Python

ใช้ os.path.exists(directory) จากโมดูล os เพื่อตรวจสอบว่าไดเรกทอรีมีอยู่หรือไม่

ตัวอย่าง

import os
if not os.path.exists('my_folder'):
    print("Given path doesn't exist")
else:
    print("Given path exists")

ถ้ารันและโฟลเดอร์มีอยู่แล้ว จะได้ข้อความ:

"Given path exists"