AppDividend
Latest Code Tutorials

How to Create Directory If Not Exist in Python

0

Creating directories programmatically is easy, but you need to make sure that it does not exist. Otherwise, you will face some problems.

Create Directory If Not Exist

To create a directory if not exist in Python, check if it already exists using the os.path.exists() method, and then you can create it using the os.makedirs() method.

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.

The os.makedirs() is a built-in Python method used to create a directory recursively.

When working with directories, we need to import the os module into our program.

import os

Now let’s define a path and check whether it exists or not.

import os

path = '/Users/krunal/Desktop/code/pyt/database'

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

Output

True

It returns True, which means it does exist.

Let’s take a scenario in which the path does not exist.

import os

path = '/Users/krunal/Desktop/code/database'

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

# Specify path
path = '/Users/krunal/Desktop/code/pyt/database/app.py'

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

Output

False

Okay. So the new modified path from the above code does not exist.

Here, we can safely create a new directory of the specified path because it does not exist.

To create a new directory, use the os.makedirs() function.

But before that, we will use the if not operator to check if it does not exist and create a new directory.

import os

path = '/Users/krunal/Desktop/code/database'

# 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!")

Output

The new directory is created!

That’s it. We successfully created a directory which does not exist previously.

If you rerun the code, it won’t create any further directories because now it exists.

The default mode is 0o777 (octal). On some systems, the mode is ignored. Where it is used, the current umask value is first masked out. If exist_ok is False (the default), an OSError is raised if the target directory already exists.

The exists_ok named argument of the makedirs() function

The os.makedirs() function takes the exists_ok argument as an optional argument because it has a default value; by default, exists_ok is set to False. For example, it will not be OK if you try to use makedirs to create an already existing path.

If you want to prevent an error message from being thrown, setting exists_ok to True when calling makedirs().

import os

path = '/Users/krunal/Desktop/code/database'

os.makedirs(path, exist_ok=False)
print("The new directory is created!")

So that’s how you easily create directories and subdirectories in Python with makedirs().

That’s it for creating a directory if not exist in Python.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.