os.mkdir(path, mode=0o777, *, dir_fd=None)
path : Path of the directorymode : Permission leveldir_fd : If dir_fd is not None, it should be a file descriptor referring to a directory, and the path to operate on should be relative; path will then be relative to that directory.
import os
path='D:\\my_dir'
os.mkdir(path) # create the directory my_dir
We can create sub directories inside by using for loop, here we are creating three level of directories ( or subdirectories). import os
drive='D:\\'
sub_path='my_dir\\'
os.mkdir(drive+sub_path) # create directory D:\my_dir
for i in range(3): # update the level required.
sub_path=sub_path+'my_dir'+str(i)+'\\'
path=drive + sub_path
print(path)
os.mkdir(path)
While creating the directories we can add files.
import os
drive='D:\\'
sub_path='my_dir\\'
os.mkdir(drive+sub_path) # create directory D:\my_dir
for i in range(3): # update the level required.
sub_path=sub_path+'my_dir'+str(i)+'\\'
path=drive + sub_path
print(path)
os.mkdir(path)
path_file=path+'my_file'+str(i)+'.txt'
print(path_file)
f = open(path_file, "w")
import os
path='D:\\my_dir'
try:
os.mkdir(path) # create the directory my_dir
except (FileExistsError):
print (" File or directory is already exists")
except (FileNotFoundError):
print (" Path is not correct ")
except OSError:
print ("Failed to create %s " % path)
else:
print ("Successfully created the directory %s " % path)
import os
path='D:\\my_dir1\\my_dir2\\my_dir3\\my_dir4'
os.makedirs(path) # create all directories in the path
Above code will raise FileNotFoundError or FileExistsError based on the condition.
import os
path='D:\\my_dir1\\my_dir2\\my_dir3\\my_dir4'
#os.makedirs(path) # create all directoris in the path
os.rmdir(path) # delete directory my_dir4
Above code will delete the directory my_dir4 only.
import os
path='D:\\my_dir\\my_dir0\\my_dir1'
my_list=os.listdir(path)
print(my_list) # ['my_dir2', 'my_file1.txt']
import os
path='D:\\my_dir1\\my_dir2\\my_dir3\\my_dir4'
#os.makedirs(path) # create all directories in the path
try:
os.rmdir(path) # delete directory my_dir4
except OSError as e:
print(e) # Specific error message
print ("Failed to delete %s " % path)
else:
print ("Successfully deleted the directory %s " % path)
Output ( my_dir4 is not available to delete )
[WinError 2] The system cannot find the file specified: 'D:\\my_dir1\\my_dir2\\my_dir3\\my_dir4'
Failed to delete D:\my_dir1\my_dir2\my_dir3\my_dir4
Create the full path by using this code ( Delete and create again if required ).
import os
path='D:\\my_dir1\\my_dir2\\my_dir3\\my_dir4'
#os.makedirs(path) # create all directories in the path
os.rmdir(path) # delete directory my_dir4
After creating directory up to my_dir4, try to delete my_dir3.
import os
path='D:\\my_dir1\\my_dir2\\my_dir3'
try:
os.rmdir(path) # delete directory my_dir3
except OSError as e:
print(e) # Specific error message
print ("Failed to delete %s " % path)
else:
print ("Successfully deleted the directory %s " % path)
Output is here.
[WinError 145] The directory is not empty: 'D:\\my_dir1\\my_dir2\\my_dir3'
Failed to delete D:\my_dir1\my_dir2\my_dir3
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.