# Tip: Making Directories Recursively With Python

> Using the OS module recursively make folders on your operating system.

- Canonical: https://dennisokeeffe.com/blog/2021-07-27-making-directories-recursively-with-python
- Published: 2021-07-27
- Updated: 2021-07-27
- Tags: beginner, tips, python, 100daysofpython

> This is Day 8 of the #100DaysOfPython challenge.

Today's post is a quick overview of making directories recursively (if they do not exist).

## Getting started

Let's create the `hello-recursive-dirs` directory and install Pillow.

```s
# Make the `hello-recursive-dirs` directory
$ mkdir hello-recursive-dirs
$ cd hello-recursive-dirs
# Create a file for our script
$ touch main.py
```

## Using the OS module to create folders recursively

The OS library has a function [`makedirs`](https://docs.python.org/3/library/os.html#os.makedirs) that can be used to make directories recursively.

In this example, let's create the folder `tmp/deep/folder`.

Add the following code to `main.py`:

```py
import os

new_folder_name = 'tmp/deep/folder'
if not os.path.exists(new_folder_name):
    os.makedirs(new_folder_name)
```

Run the script with `python main.py` and confirm that the folder was created.

## Resources and further reading

1. [`OS.makedirs`](https://docs.python.org/3/library/os.html#os.makedirs)

_Photo credit: [pawel_czerwinski](https://unsplash.com/@pawel_czerwinski)_
