Create directory recursively 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.,

Create a single directory in Python

You can create a single directory in python using the os.mkdir() method.

Syntax

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

The path is the name of the folder either referring to the absolute or relate path depending on how you want it to work.

import os
# new folder/dir name
new_directory = "debugpointer"

# Parent Directory path
# In case of Windows, "D:/"
parent_directory = "/home/ubuntu/"

# Setting the path for folder creation
path = os.path.join(parent_directory, new_directory)

mode = 0o777

# Create the directory in the path
os.mkdir(path, mode)
print("Directory %s Created Successfully" % new_directory)

Output-

Directory debugpointer Created Successfully

Note - FileExistsError is raised when the file already exists in the path. It is advised to handle this error in cases where folder are created often(maybe in a python script that runs every time or the API which creates a folder for some reason, etc.,)

Create directories recursively using in Python

The os.makedirs() method creates a directory recursively in a given path in Python. This means, you can create folders with-in folders (with-in folder and so on...) easily using the os.makedirs() method.

os.makedirs(path, mode = 0o777, *, dir_fd = None)

Suppose you want to create 3 folders one within another in the form - debugpointer => python => posts, you can easily achieve this using the os.makedirs() method. Python makes care of creating the recursive folders for you when you just specify the structure of your folders that you need.

import os
# new folder/dir name
new_directory = "debugpointer/python/posts"

# Parent Directory path
# In case of Windows, "D:/"
parent_directory = "/home/ubuntu/"

# Setting the path for folder creation
path = os.path.join(parent_directory, new_directory)

mode = 0o777

# Create the directory in the path
os.makedirs(path, mode)
print("Directory %s Created Successfully" % new_directory)

Output-

Directory debugpointer/python/posts Created Successfully

Note - FileExistsError is raised when the file already exists in the path. It is advised to handle this error in cases where folder are created often(maybe in a python script that runs every time or the API which creates a folder for some reason, etc.,)

In such cases, you can handle other OSError errors as follows and also use exist_ok = True so that the FileExistsError does not appear and it gets suppressed -

import os
# new folder/dir name
new_directory = "debugpointer/python/posts"

# Parent Directory path
# In case of Windows, "D:/"
parent_directory = "/home/ubuntu/"

# Setting the path for folder creation
path = os.path.join(parent_directory, new_directory)

# Handle the errors
try:
	# Create the directory in the path
	os.makedirs(path, exist_ok = True)
	print("Directory %s Created Successfully" % new_directory)
except OSError as error:
	print("Directory %s Creation Failed" % new_directory)

Output-

Directory debugpointer/python/posts Created Successfully