Dennis O'Keeffe
Aug 29, 2021
Heading image

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.

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 main.py:

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

Resources and further reading

  1. OS.makedirs

Photo credit: pawel_czerwinski

Originally posted on my blog. To see new posts without delay, read the posts there and subscribe to my newsletter.

I write content for AWS, Kubernetes, Python, JavaScript and more. To view all the latest content, be sure to visit my blog and subscribe to my newsletter. Follow me on Twitter.

Dennis O'Keeffe
Dennis O'Keeffe

No responses yet