30: How to Check if a Directory Exists or Not in Python Program


Question 30) Write a program in Python to check if a directory exists or not.


Solution:
'''

Python Program 30:
  Check if directory exists or not

Author: Code2care.org
Version: v1.0
Date: 06-07-2023


'''
import os

directory_path = "/Users/c2ctech/Desktop"
is_directory_exists = os.path.exists(directory_path)

if is_directory_exists == True:
  print(f"The Directory Exists at path : {directory_path} ")
else:
   print(f"The Directory Does Not Exists at path : {directory_path} ")
Output:
The Directory Does Not Exists at path : /Users/c2ctech/Desktop 
Check if a Directory Exists or Not in Python Program

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap