How to create a directory recursively using Python?


In Python programming, operations on files and directories are routine tasks that you carry out daily and are a common requirement. Having a rich set of libraries and intuitive syntax, Python provides a simple and straightforward way to carry out such tasks. Here, in this article, we will explore and learn how to create directories recursively using Python. It does not matter if you are either a beginner or an experienced developer, this article will guide you in a step-by-step manner to help you acquire this essential skill. So, let's start acquiring the knowledge of creating directories effortlessly using Python!

Understanding Directory Structure

Before we start creating directories, let us first attempt to understand the concept of directory structure. A directory is a folder that further contains more files and subdirectories. Directories are organized hierarchically, resembling a tree-like structure. The task of creating directories recursively means creating not only the target directory but also creating any missing parent directories leading up to it.

The os Module in Python

To interact and work with the operating system and carry out file and directory operations in Python, we use the os module. This module provisions for a wide range of functions and methods to manipulate directories, files, and paths.

Checking if a Directory Exists

Before we create a directory, it's a good idea to check if it already exists. The os.path.exists() function is used for this purpose. Here's an example:

Example

import os

directory = '/path/to/directory'

if not os.path.exists(directory):
   print("Directory does not exist")
else:
   print("Directory already exists")

Output

For one particular directory, the output can be

Directory does not exist

In the above given code, we verify and check if the directory at any given path exists or unit making use of the os.path.exists() function.

Creating a Directory

To make a directory or folder in Python, we also use the os.mkdir() function. However, this function creates a single directory at a time. To make directories recursively, we should use the os.makedirs() function. Here is how it done in the example given below:

import os

directory = '/path/to/new/directory'

os.makedirs(directory)

The os.makedirs() function makes the directory at the given path, including any missing parent directories.

Handling Exceptions

When directories are created, it's important to handle exceptions and errors that may happen. For example, if the parent directory is read-only or if the user has insufficient permissions, an exception will be raised. In such a scenario, we can use the try-except block to handle such exceptions effectively. Let us consider an example below:

Example

import os

directory = '/path/to/new/directory'

try:
   os.makedirs(directory)
   print("Directory created successfully")
except OSError as e:
   print(f"Error: {e}")

Output

For one particular new directory, the output can be

Error: [Errno 13] Permission denied: '/path'

In the above code, we make an attempt to create the directory, and if an exception happens, we try to catch it and display an error message.

Putting It All Together

By now we have explored the basics; let us put our acquired knowledge into use by creating a complete Python script to create directories recursively:

Here, in the code below, we declare a function create_directory() that takes a directory path as an argument and makes an attempt to create the directory using os.makedirs(). We handle any exceptions or errors that may occur and provide befitting feedback to the user.

Example

import os

def create_directory(directory):
   try:
      os.makedirs(directory)
      print(f"Directory '{directory}' created successfully")
   except OSError as e:
      print(f"Error: {e}")

# Example usage
create_directory('/path/to/new/directory')

Output

For one particular new directory, the output can be

Directory '/content/paloma' created successfully

It is obvious that by now you have now learned how to create directories recursively using Python. We have explored the os module and its functions for directory creation operations.

Making Use of Pathlib Module

The pathlib module in Python, on the other hand, provides an object-oriented approach to handle file system paths. Let us see how you can create directories recursively using pathlib functions and methods:

In this code example, we create a Path object pointing to the target directory. The mkdir() method is called on the directory object with the parents=True argument, and this enables the creation of parent directories if they don't happen to exist. The exist_ok=True argument makes sure that an exception is not raised if the directory already exists.

from pathlib import Path
directory = Path('/path/to/new/directory')
directory.mkdir(parents=True, exist_ok=True)

Make Use of os.makedirs() with Multiple Directories

In some instances, you might want to create several directories all at once. You can obtain this by passing a list of directory paths to the os.makedirs() function. Let us consider an example:

In the code below, we specify a list of directory paths and make use of a 'for loop' to iterate over each directory. The os.makedirs() function is called for each directory, and the exist_ok=True argument ensures that an exception is not raised if any of the directories already exist.

import os

directories = ['/path/to/new/directory1', '/path/to/new/directory2', '/path/to/new/directory3']

for directory in directories:
   os.makedirs(directory, exist_ok=True)

By incorporating the practice of these code examples into your skill set, you now have a broad understanding of how to create directories recursively using Python. You can either choose the os module or the pathlib module; it is up to you to do so. In any case, you have the flexibility to choose the approach that best fits your coding style and requirements.

It is better to handle exceptions properly to make sure smooth execution of code and further explore other functionalities provisioned by the os and pathlib modules to enhance your directory manipulation capabilities or skills.

Updated on: 17-Jul-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements