# This requires Python’s OS module
import os
# 'mkdir' creates a directory in current directory.
os.mkdir('tempDir')
# can also be used with a path, if the other folders exist.
os.mkdir('tempDir2/temp2/temp')
# 'makedirs' creates a directory with it's path, if applicable.
os.makedirs('tempDir2/temp2/temp')
import os
if not os.path.exists(directory):
os.makedirs(directory)
newpath = r'C:Program Filesarbitrary'
if not os.path.exists(newpath):
os.makedirs(newpath)
import os
# define the name of the directory to be created
path = "/tmp/year"
try:
os.mkdir(path)
except OSError:
print ("Creation of the directory %s failed" % path)
else:
print ("Successfully created the directory %s " % path)
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))
# Make directory path (recursive) if it does not exist
# If exists then no exception is thrown
# Analogous to mkdir -p
os.makedirs("<<SOME_PATH>>", exist_ok=True)
import os
if not os.path.exists('parentdirectory/mydirectory'):
os.makedirs('parentdirectory/mydirectory')
>>> os.mkdir('test')
>>> os.listdir()
['test']
from pathlib import Path
Path("/my/directory").mkdir(parents=True, exist_ok=True)