Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Python Script To Check If A Directory Exists, If Not, Create It

1 min read

In this article, we will create a Python script which will check if a particular directory exists on our machine or not if not then the script will create it for us with built in Python functions.

Check If A Directory Exists, If Not, Create It

The OS module in python provides functions for interacting with the operating system. OS, comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

os.path.isdir(): Method used for checking if a given directory exists or not.
os.makedirs(): The method used for creating a directory.

Program

'''Check if directory exists, if not, create it'''
import os

# You should change 'test' to your preferred folder.

MYDIR = ("test")
CHECK_FOLDER = os.path.isdir(MYDIR)

# If folder doesn't exist, then create it.
if not CHECK_FOLDER:
    os.makedirs(MYDIR)
    print("created folder : ", MYDIR)
else:
    print(MYDIR, "folder already exists.")

Output

created folder :  test

Explanation

First, we are importing the OS module next we are declaring the directory which we will look for you should change it to your preferred directory or folder name.

Then we are using the os.path.isdir(MYDIR) method and looking for our folder on the system and below that we have a set of conditional operation which creates a new directory with a given name using os.makedirs(MYDIR) method if the isdir() method evaluates to false.

Note that the folder or directory will be generated at the location where the script exists.


PROGRAMS

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read