create a new directory in Python code example
Example 1: python create directory
import os
os.mkdir('tempDir')
os.mkdir('tempDir2/temp2/temp')
os.makedirs('tempDir2/temp2/temp')
Example 2: create a directory python
import os
def create_dir(dir):
if not os.path.exists(dir):
os.makedirs(dir)
print("Created Directory : ", dir)
else:
print("Directory already existed : ", dir)
return dir
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))