Skip to content

Python os mkdir Recursive

  • by

In Python, the os module provides a function called mkdir() that can be used to create directories. By default, mkdir() creates a single directory, but you can use the os.makedirs() function to create directories recursively.

Python os mkdir Recursive example

Here’s an example of how to use os.makedirs() to create directories recursively:

import os

# Create a directory named "parent_directory" in the current working directory

# Create a directory named "child_directory" inside "parent_directory"


Python os mkdir Recursive

In the above example, os.makedirs() is used to create a directory named “parent_directory” in the current working directory. Then, it creates another directory named “child_directory” inside the “parent_directory”.

The os.makedirs() function creates all the intermediate directories if they don’t exist. So, in this example, if “parent_directory” doesn’t exist, it will be created first, and then “child_directory” will be created inside it.

You can specify the full path to the directory you want to create, including multiple levels of directories, and os.makedirs() will create them all recursively if needed.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *