Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Create Directory If It Does Not Exist in Python

  • 14 Apr, 2025
  • Com 3
Creating a directory if it does not exist in Python

To create a directory if it does not exist in Python, the most recommended approach is to use the os.makedirs() function with the exist_ok=True argument. It also avoids race conditions, so it is safe in multi-threaded environments.

The os.makedirs() can also create intermediate directories if they don’t exist.

If the directory exists, then it will throw a FileExistsError exception.

Before creating a directory, our “parent” directory looks like this:

Current directory status

You can see that it’s empty.

Let’s write a program to create a directory inside the above empty directory:

import os

dir_path = "parent/child/grandchild"

try:
    os.makedirs(dir_path, exist_ok=True)
except OSError as e:
    print(f"Error: {e}")

Using os.makedirs() to create a directory

The above output screenshot of a file system shows that it created a child directory inside the parent directory and another nested directory called grandchild inside the child directory. That means makedirs() is a recursive function that can be used to create nested directories, too!

You can see that we passed the exist_ok argument, which prevents the function from raising an error by setting it to True even if the directory already exists.

If you rerun this code, it won’t throw any error. However, if you set the exist_ok to False, it throws FileExistsError.

import os

dir_path = "parent/child/grandchild"

try:
    os.makedirs(dir_path, exist_ok=False)
except OSError as e:
    print(f"Error: {e}")

# Output: Error: [Errno 17] File exists: 'parent/child/grandchild'

So, it is better to set exists_ok to True.

There is still a chance that it will throw an error like FileExistsError or PermissionError:

  1. If the path is an existing file (not a directory), it will throw FileExistsError.
  2. If the directory does not have proper permissions, it will throw a PermissionError.

You can pass the “mode=0o777” argument to handle the permission. It works on Unix-style systems like 0o755 for read/write/execute for the owner.

os.makedirs(dir_path, exist_ok=False, mode=0o777)

Bulk directory creation

If you want to create directories in bulk, you should use a for loop with exist_ok=True for efficiency:

import os

dir_paths = ["dir1", "dir2/subdir", "dir3"]

try:
    for path in dir_paths:
        os.makedirs(path, exist_ok=True)
except OSError as e:
    print(f"Error: {e}")

Using pathlib (Python 3.4+)

There is a built-in module called pathlib, which includes a Path.mkdir() method that you can use if you are using Python version 3.4 or later. It is more readable and cross-platform.

from pathlib import Path


dir_path = Path("parent/child/grandchild")

try:
    dir_path.mkdir(parents=True, exist_ok=True)
except FileExistsError:
    print(f"Error: {dir_path} is a file, not a directory.")
except PermissionError as e:
    print(f"Permission denied: {e}")

In the above code, we passed the parents=True argument, which tells the interpreter to create parent directories if they are missing.

The exist_ok=True argument tells no error if the directory exists.

It also avoids race conditions by default and handles path separators automatically (e.g., \ vs. /).

Additionally, this approach may encounter FileExistsError or PermissionError. For that, you have to deal with it manually.

For example, you can use the os.path.isfile() method to check if it is a file. That will handle the FileExistsError.

To handle PermissionError, grant proper permissions, run the command as an administrator, or select a new path.

Using os.path.exists() and os.makedirs() (Not recommended)

You can use the os.path.exists() method to check the directory’s existence and then use the os.makedirs() function to create it, but it is inefficient and creates a race condition.

Now, you may ask, what is a race condition? Well, if you check os.path.exists() function and then create the directory, another process could create/delete the directory in between, leading to unexpected behavior. Within microseconds, another process can create a directory, which causes issues.

So, it is not safe in multi-threaded environments.

Now, you will ask another question:. Where is the need for using this approach, then?

If you have to execute specific logic only if the directory did not exist before creation, you might combine os.path.exists() with the os.makedirs() method.

import os

dir_path = "parent/child/grandchild"

if not os.path.exists(dir_path):
    os.makedirs(dir_path)
    print("Directory created from scratch!")
else:
    print("Directory already existed.")

# Output: Directory created from scratch!

That’s it!

Post Views: 59
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Reverse a Range in Python
Creating Temporary Files and Directories Efficiently in Python

3 Comments

  1. SoftBigs

    August 4, 2023 at 6:02 am

    I found this article helpful. Thank you for writing it!

    Reply
  2. John Davis

    March 29, 2024 at 12:09 pm

    Great blog post! I’m glad to see a detailed explanation of how to create a directory if it doesn’t exist in Python. This is a useful technique that I will definitely use in my own projects. Thanks for sharing!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend