Explain Codes LogoExplain Codes Logo

How can I create directories recursively?

python
pathlib
filesystem
directory-creation
Anton ShumikhinbyAnton Shumikhin·Feb 9, 2025
TLDR

For recursive directory creation in Python, utilize the built-in function os.makedirs() with the attribute exist_ok=True. This ensures directories created without raising an exception if they already exist:

import os # The modern way to create a treehouse... digitally os.makedirs('desired/path/to/treehouse', exist_ok=True)

An alternative approach: pathlib

The pathlib module, introduced in Python 3.4, offers a more high-level, object-oriented interface to filesystems. Here's how you'd do the same task with pathlib:

from pathlib import Path # Don't worry, this `Path` leads somewhere nice Path('desired/path/to/treehouse').mkdir(parents=True, exist_ok=True)

A deeper dive: using os and subprocess modules

When it comes to handling permissions or using shell-like commands, Python has built-in solutions. The os module contains functions for changing permissions and owners, and subprocess can be used to invoke shell commands:

import os import subprocess # Mode 0o755 gives the owner full permission and others read and execute permissions os.makedirs('desired/path', mode=0o755, exist_ok=True) # Or using subprocess to evoke shell commands subprocess.run(['mkdir', '-p', path]) subprocess.run(['chmod', '-R', '755', path]) # Safety first! sanitizing input is a must with shell commands

Creating your custom function

In certain cases, you may find the need to customise the directory creation process or add more logic. Here's a simple custom function:

import os def custom_directory_creator(path): if not os.path.exists(path): os.makedirs(path) else: print(f"Directory {path} already exists, stuff your data in there!") # And this is how you use it custom_directory_creator('desired/path')

In this function, a check is made to ensure the directory does not already exist before creation. You can modify this function to suit any of your special needs.

Pathlib's other talents

The pathlib module isn't a one-trick pony. It also provides several methods for working with files, including checking for directory existence:

from pathlib import Path # Create path object path = Path('desired/path/to/treehouse') # Check if directory exists if not path.is_dir(): path.mkdir(parents=True, exist_ok=True)

Avoid common mistakes

Beware of filesystem race conditions. If a directory is created by another process just after you checked for its existence but before you create it, an error will occur. To prevent this, use exist_ok=True.

Avoid os.mkdir() without exist_ok=True or a check for existence, as it raises a FileExistsError if the directory exists.

When using subprocess, avoid concatenating strings to create command-line arguments. Use list arguments to prevent command injection vulnerabilities.