This is Day 8 of the #100DaysOfPython challenge.
Today's post is a quick overview of making directories recursively (if they do not exist).
Let's create the
hello-recursive-dirs directory and install Pillow.
# Make the `hello-recursive-dirs` directory $ mkdir hello-recursive-dirs $ cd hello-recursive-dirs # Create a file for our script $ touch main.py
The OS library has a function
makedirs that can be used to make directories recursively.
In this example, let's create the folder
Add the following code to
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.
Photo credit: pawel_czerwinski
Originally posted on my blog. To see new posts without delay, read the posts there and subscribe to my newsletter.