There are several methods to perform this task, I will discuss a few with you:
You can try os.path.existsand for creation consider os.makedirsfor creation.
import os
if not os.path.exists(directory):
os.makedirs(directory)
If you face an error you can trap and examine it using embedded error code
import os, errno
try:
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise
2. If you are using Python 3.5+ you can use pathlib, it creates directory without raising an exception if directory already exists. I you want to create the parents or don't need it, then skip the parents argument
import pathlib
pathlib.Path('/my/directory').mkdir(parents=1, exist_ok=1)
To know more about this you can have a look at the following video tutorial:-
Learn in detail about Python by enrolling in Intellipaat Python Course online and upskill.