Skip to main content
How To Create Directory If Not Exist In Python

How To Create a Directory If Not Exist In Python

in this quick python tutorial, We will learn how to create a Directory if it Does Not Exist using Python. We’ll use the python OS module to check the directory and create using the inbuilt method.

Creating a directory in Python is relatively simple. We can do this in two ways: either using the os.makedirs() method or the os.mkdir() method.

The os.mkdir() method is used to create a single directory, while the os.makedirs() method is used to create multiple directories at once.

Checkout other recommendable tutorials:

Using os.mkdir()

os.mkdir() method in Python is used to create a directory named path with the specified numeric mode. This method raise FileExistsError if the directory to be created already exists.

Syntax:

os.mkdir(path[, mode])
  • path − The path is the name of the directory you want to create.
  • mode − This is the mode of the directories to be given.

Return Value: This method does not return any value.

os.makedirs()

The os module has in-built os.makedirs() method, it’s used to recursively construct a directory. That is if any intermediate-level directory is missing while creating the leaf directory, the os.makedirs() method will construct all of them.

Syntax :

os.mkdir(path, mode = 0o777, *, file_descriptor = None)

  • path (required): Where we want to create a directory.
  • mode (optional): It is an integer value representing a mode of a directory to be created.
  • file_descriptor(optional): This parameter has the value None by default. The file descriptor parameter is ignored if the path is absolute.

os.path.exists()

The os.path.exists() is a built-in Python method that is used to check whether the specified path exists or not. The os.path.exists() method returns a boolean value which is either True if the path exists otherwise returns False.

Check Directory If Not Exist

We’ll use the os.path.exists() method to see if a directory already exists. Let’s check directory exists or not:

import os

path = '/usr/share/pythonpip'

# Check whether the specified path exists or not
isExist = os.path.exists(path)
print(isExist)

Output :

True

It returns True, indicating that it exists.

Check Path does not exist

Consider the case where the path does not exist.

import os

path = '/usr/share/pythonpip/tmp'

# Check whether the specified path exists or not
isExist = os.path.exists(path)
print(isExist)

Output:

False

The modified path from the above code does not exist and return False.

Create A Directory Using os.mkdir()

Let’s create a pythonpip directory using os.mkdir().The os.mkdir() method takes an argument, the path of the directory to be created.

import os, sys

# Path to be created
path = "/usr/share/pythonpip"

os.mkdir( path, 0755 );

print("Directory '%s' has been created" %path)

Output:

Directory /usr/share/pythonpip has been created

Create a Directory Using os.path.exists() and os.makedirs() methods

We’ll use the os.makedirs() function to create a directory in python. We will use the if not operator to check if it does not exist and create a new directory.

import os

path = '/usr/share/pythonpip'

# Check whether the specified path exists or not
isExist = os.path.exists(path)

if not isExist:
  
  # Create a new directory because it does not exist 
  os.makedirs(path)
  print("The new directory is created Successfully.!")

Output:

The new directory is created Successfully.!

Create a Directory Using  isdir() and os.makedirs() methods

The isdir() method takes the path of a directory as an argument and returns true if the directory exists. If the directory does not exist, it returns false. We can also use the exists() method to check if a directory exists.

The below script will use to create a Directory if it Does Not Exist using Python.

import os


# checking if the directory pythonpip
# exist or not.
if not os.path.isdir("usr/share/pythonpip"):
	
	# if the pythonpip directory is
	# not present then create it.
	os.makedirs("usr/share/pythonpip")

Leave a Reply

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