Python create directory if it does not exist

Share This Post

Python, being the versatile language it is, allows programmers to create directories using its OS module. This module comes under the standard modules of python. Python creates the directory if it does not exist The function of this module is to provide a portable way of using operating system-dependent functionality.

Other than that the os and os. path modules include many functions to interact with the file system. In case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted by the operating system, all functions of the OS module will raise an OSError.

Python creates directory if it does not exist

Furthermore, the two methods that are available in the OS module for creating a directory are:

  1. os.mkdir
  2. os.makedirs


The os.mkdir method in python is used to create a directory named path using a specified numeric mode. Furthermore, this method will raise a “fileexistserror” if the directory that you want to create already exists. The syntax for this method is os.mkdir(path, mode = 0o777, *, dir_fd = None). A sample code is giving below for your help:

import os
directory = "test"
parent_dir = "D:/Pycharm projects/"
path = os.path.join(parent_dir, directory)
print("Directory '% s' created" % directory)
directory = "Hello"
parent_dir = "D:/Pycharm projects"
mode = 0o666
path = os.path.join(parent_dir, directory)
os.mkdir(path, mode)
print("Directory '% s' created" % directory)


Directory ‘test’ created

Directory ‘Hello’ created


This method comes in handy when a programmer wants to create a directory recursively. In other words, os.makedirs will create all intermediate-level directories if they are missing while creating a leaf directory.

Carefully study the following example for better understanding:

Consider the directory given below:
D:/Pycharm projects/test/Authors/Owais

What if we want to create a directory called Owais but the directories of ‘test’ and ‘Authors’ are unavailable in the path? You can do so by using the os.makedirs method.

This method will automatically create all missing directories in a specified path. Therefore, the “test” and “Authors” directories will be created before the “Owais” directory. The following example shows the correct use of this method:

import os 
directory = "Owais" 
parent_dir = "D:/Pycharm projects/test/Authors"
path = os.path.join(parent_dir, directory) 
print("Directory '% s' created" % directory) 
directory = "c"
"D:/Pycharm projects/test/a/b" 
mode = 0o666    
path = os.path.join(parent_dir, directory)      
os.makedirs(path, mode) 
print("Directory '% s' created" % directory) 
# If any of the intermediate level 
 directory is missing os.makedirs() method will 
 create them os.makedirs() method can be used to create a directory tree

Another example is given below:

import os 
directory = "Nikhil"
parent_dir = "D:/Pycharm projects/GeeksForGeeks/Authors"
path = os.path.join(parent_dir, directory) 
print("Directory '% s' created" % directory) 

Read More: The Best Way to Strip Punctuation from a String using Python.

Creating a directory (folder) in Python if it does not exist is relatively simple using the os module. For example, let’s create a directory called “test” in the current working directory:

import os

if not os.path.exists('test'):

Now, if we check the contents of our current working directory, we should see the new “test” directory.

This is all that is needed to create a new directory in Python if it does not exist.


Related Posts

Python for Loop Iteration: How to Easily Manipulate a List

One of the most powerful features of the Python...

How To Get First Characters of a String in Python

Way to find the first characters of a string...

How To Convert Tuple To String In Python

In this post, you will learn how to convert...

How To Convert String to Double in Python

Good to hear, that you want to convert a...

Python TypeError: String Index Out Of Range Solution

A string is an array of characters and part...

How To Remove Empty Strings From a List Of Strings

On this page, we will discuss how to remove...
- Advertisement -spot_img