As a Python developer, I’ve often found myself in situations where I need to create directories dynamically within my code. However, there’s always the concern that the directory might already exist, leading to errors or unwanted behavior. That’s where the beauty of Python shines through. In this blog post, I’ll walk you through the process of creating a directory if it does not exist in Python. With this knowledge, you’ll be able to handle directory creation seamlessly and ensure smooth execution of your code.
The os
module provides functions for interacting with the Operating System in Python. The os
module is standard python utility module. Most Operating System based functionalities are abstracted out in this module and it is provided in a very portable way.
Note – In case of any error or exception, all the functions in the os
module raise an OSError
. Examples are – trying to create an invalid file name(or folder name), incorrect arguments to the functions, etc.,
Method 1: Using os.path.exists and create the folder if it doesn’t exist
Step 1: Check if the folder exists using os.path.exists
Let’s achieve this in 2 steps, firstly we will use the os.path.exists() method to see if a directory already exists, and then let’s use the os.makedirs() method to create the directory.
You can check if a directory already exists in the path by checking the following os.path.exists(path)
.
In the code below we will check if the directory path
exists. os.path.exists
returns a boolean value which is then printed.
import os
path = "path-to-directory"
isExist = os.path.exists(path)
print(isExist)
Output
False
Since the path to the directory didn’t exist it returned False
. In case the directory exists, it’ll return True
.
Step 2: Create directory conditionally if it doesn’t exist
Here we will extend the above code, the isExist
variable will help us to determine if we have to create a directory conditionally.
import os
path = "path-to-directory"
isExist = os.path.exists(path)
if not isExist:
os.makedirs(path)
print('Successfully created directory')
Output
Successfully created directory
The os.makedirs()
creates the directory recursively. In case you are specifically looking to create recursive directories in python check the article, where we cover various examples on this topic.
Method 2: Using os.path.isdir and create the folder if it doesn’t exist
Step 1 : Check if the path is a directory using os.path.isdir
Let’s achieve this in 2 steps, firstly we will use the os.path.isdir() method to see if the path is a directory, and then let’s use the os.makedirs() method to create the directory.
In the code below we will check if the path path
is a directory. os.path.isdir
returns a boolean value which is then printed.
import os
path = "path-to-directory"
isDir = os.path.isdir(path)
print(isDir)
Output
False
Since the path to the directory didn’t exist it returned False
. In case the directory exists, it’ll return True
.
Step 2 : Create directory conditionally if it doesn’t exist or is not a directory
Here we will extend the above code, the isDir
variable will help us to determine if we have to create a directory conditionally.
import os
path = "path-to-directory"
isDir = os.path.isdir(path)
if not isDir:
os.makedirs(path)
print('Successfully created directory')
Output
Successfully created directory
And there you have it! We’ve reached the end of our journey on how to create a directory if it does not exist in Python. It’s a simple yet powerful technique that can save you from potential headaches and errors. By employing the techniques we’ve covered, you can confidently create directories on the fly and organize your data effortlessly. Python continues to amaze with its flexibility and ease of use, empowering developers like us to achieve great things. Happy coding and may your directories always exist exactly where you need them!