Working with directories is a fundamental task in Python programming. Whether you’re building a file organizer, managing downloads, or handling project files, you need to create and delete folders programmatically. In this tutorial we will learn using python OS module we can create and delete folders / directories, we will also using python function.
Python function to create and delete folders Code
Create a file named folder.py
import os
cpath=os.getcwd()
def folderO(option, folderName, cpath):
path=cpath+"/"+folderName
#print(path)
if option=="C":
if os.path.exists(path):
print(f"Folder {folderName} already exist")
else:
os.mkdir(folderName)
print(f"Folder {folderName} created successfully")
if option=="D":
if os.path.exists(path):
os.rmdir(folderName)
print(f"Folder {folderName} deleted successfully")
else:
print(f"Folder {folderName} not existed")
option=input("For create a folder enter C, to delete enter D:\n")
folderName=input("Enter Folder Name, don't use space and special character:\n")
#print(option)
#print(folderName)
folderO(option, folderName, cpath)
Python function to create and delete folder Screen shot

If you have and query or suggestion or need assistance then please contact me using comments, I will reply to fix your problem, if you like our content then you can subscribe to our Youtube channel. If you want to hire me then reach us at [email protected].








