In this article, we will see how to create a directory if the specified directory does not exist using python.
In Python, we can create a directory/folder programmatically with the help of the os
module in python.
However, before making a new directory it is important for us to know if the same directory already exists or not in our project folder. If not then we will create the directory using python.
Check if the directory exists using python
Before proceeding to create a directory in python we will first check if it already exists using the os.path.exists() method of the os module.
The os
module in python is a utility module that provides functions to interact with the operating system.
The os.path.exists()
method is an in-built function that is used to check if a specific path of a directory exists or not. If the path exists, then the method will return True
, else it will return False
.
Let’s see an example to understand it better.
Suppose this is the file and folder structure of our project.
📦myproject
┣ 📂output
┃ ┗ 📂document
┃ ┃ ┗ 📜doc1.txt
┗ 📜main.py
Now let’s check if a specified directory exists or not using the os
module and os.path.exists()
method.
import os
#path of the document directory
dir_path = './output/document'
result = os.path.exists(dir_path)
print(result)
Output:
True
That means the directory exists in the project folder.
Now, let’s check a folder that does not exist in our project.
import os
#path of the picture directory
dir_path = './output/picture'
result = os.path.exists(dir_path)
print(result)
Output:
False
Since the picture folder/directory does not exist in our project the method returns False
.
Next, let’s check how we can create a directory using python programmatically.
Create a directory if it does not exist in Python
In python, to create a new directory, we use the os.makedirs()
function.
Now, when we know that a specified directory does not exist, then we can use the makedirs()
method and create the directory with the specified name.
Syntax:
os.makedirs(path, mode = 0o777, exist_ok = False)
The path is the file system path to create the new directory.
The mode is an optional parameter. It is an integer value that represents a newly created directory using makedirs() method. The default value is 0o777.
The exist_ok=False is also optional, Its set to False by default. If the value is False the OSError will raise if the targeted directory already exists.
Now let’s write a python program that will first check if the specified directory already exists. If not, then it will create the directory.
We will use the not
operator to check if the directory exists or not.
import os
#path of the picture directory
dir_path = './output/picture'
dir_exist = os.path.exists(dir_path)
if not dir_exist:
os.makedirs(dir_path)
print('Directory created successfully')
else:
print('Directory already exists')
Output:
Directory created successfully
Since the picture folder does not exist, the dir_exist returns False
.
And when we use the not
operator on the dir_exist
value, the condition returns True
and so the first statement is executed and we create the directory successfully.
Note: The
not
operator inverse the truth value of boolean expression i.e the False will be turnedTrue
and the True will be turned toFalse
.
We can also use write the program using the try except block.
import os
#path of the picture directory
dir_path = './output/picture'
dir_exist = os.path.exists(dir_path)
try:
os.makedirs(dir_path)
print('Directory created successfully')
except FileExistsError:
print('Directory already exist')
It will execute the try block first and if the folder does not exist, then it will create the folder, else the except block will execute throwing us the error message.
Once we have successfully run the program, the os.makedirs()
method creates the picture directory in our output folder programmatically.
📦myproject
┣ 📂output
┃ ┣ 📂document
┃ ┃ ┗ 📜doc1.txt
┃ ┗ 📂picture
┗ 📜main.py
So this is how we can create directories and subdirectories easily in python.
Other Articles You’ll Also Like: