how to create a directory if not exists in python code example
Example 1: python create new folder if not exist
import os
if not os.path.exists('my_folder'):
os.makedirs('my_folder')
Example 2: python make directory if not exists
try:
os.makedirs("path/to/directory")
except FileExistsError:
# directory already exists
pass
Example 3: create directory in python
import os
directory = "Krishna"
path_dir = "C:/Users/../Desktop/current_dir/"
if not os.path.exists(directory):
os.mkdir(os.path.join(path_dir, directory))