mkdir -p functionality in Python
The os.makedirs()
function in Python can be used to create a directory with the -p
option, which will create the entire directory path, including any missing parent directories. Here is a code snippet:
import os
directory_path = '/path/to/directory'
os.makedirs(directory_path, exist_ok=True)
Watch a video course
Python - The Practical Guide
The exist_ok
parameter is set to True
, so if the directory already exists, os.makedirs()
will not raise an error. If you want to raise an error if the directory already exists, you can set exist_ok
to False
import os
directory_path = '/path/to/directory'
os.makedirs(directory_path, exist_ok=False)
This will create the directory if it does not exist and raise an error if it already exists.