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 is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap