1# This requires Python’s OS module
2import os
3
4# 'mkdir' creates a directory in current directory.
5os.mkdir('tempDir')
6# can also be used with a path, if the other folders exist.
7os.mkdir('tempDir2/temp2/temp')
8
9# 'makedirs' creates a directory with it's path, if applicable.
10os.makedirs('tempDir2/temp2/temp')
1#creates a directory without throwing an error
2import os
3def create_dir(dir):
4 if not os.path.exists(dir):
5 os.makedirs(dir)
6 print("Created Directory : ", dir)
7 else:
8 print("Directory already existed : ", dir)
9 return dir
10
1import os
2
3# define the name of the directory to be created
4path = "/tmp/year"
5
6try:
7 os.mkdir(path)
8except OSError:
9 print ("Creation of the directory %s failed" % path)
10else:
11 print ("Successfully created the directory %s " % path)
12
1newpath = 'C:\Program Files\arbitrary'
2if not os.path.exists(newpath):
3 os.makedirs(newpath)
4
1newpath = r'C:\Program Files\arbitrary'
2if not os.path.exists(newpath):
3 os.makedirs(newpath)
1import os
2directory = "Krishna"
3path_dir = "C:/Users/../Desktop/current_dir/"
4if not os.path.exists(directory):
5 os.mkdir(os.path.join(path_dir, directory))