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')
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 = r'C:\Program Files\arbitrary'
2if not os.path.exists(newpath):
3 os.makedirs(newpath)
1newpath = 'C:\Program Files\arbitrary'
2if not os.path.exists(newpath):
3 os.makedirs(newpath)
4
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))
1# Python program to create directory using os.makedirs() method
2
3import os
4
5# Directory path
6dir_path = "C:/Projects/Tryouts/test/sample/mydir"
7os.makedirs(dir_path)
8print("Directory '% s' created" % dir_path)
9
10
11# Directory path
12dir_path2 = "C:/Projects/Tryouts/test/sample/mydir2"
13# mode
14mode = 0o666
15os.makedirs(dir_path2, mode)
16print("Directory '% s' created" % dir_path2)
17