DebugPointer
Published on

Create directory if it does not exist in Python

Create directory if it does not exist in Python

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 directory 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 directory 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

As you see in the above code examples, it's pretty simple to create folders if it doesn't already exist in Python.