How to create a folder if it doesn't exist in Python

1 min

I don't remember the number of times I needed to do this.

This is extremely useful, especially when you want to algorithmically generate a folder structure in Python.

Here is the code

import os
if not os.path.exists('my_folder'):
    os.makedirs('my_folder')
How to create a folder if it doesn't exist using Python

Here you are! You now know how to create a folder if it doesn't exist in Python.

More on fundamentals

If you want to know more about Python fundamentals without headaches... check out the other articles I wrote by clicking just here:

Fundamentals - The Python You Need
We gathered the only Python essentials that you will probably ever need.