Back to home

Getting startedUsing the OS module to create folders recursivelyResources and further reading
Tip: Making Directories Recursively With Python main image

Tip: Making Directories Recursively With Python

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.

1 2 3 4 5 # Make the `hello-recursive-dirs` directory $ mkdir hello-recursive-dirs $ cd hello-recursive-dirs # Create a file for our script $ touch

Using the OS module to create folders recursively

The OS library has a function 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

1 2 3 4 5 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 and confirm that the folder was created.

Resources and further reading

  1. OS.makedirs

Photo credit: pawel_czerwinski

Dennis O'Keeffe

  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on, Den Dribbles and LandPad .

Related articles


Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.