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

References:

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!