Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

Python function to create and delete folders

Python function to create and delete folders

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

Python function to create and delete folders

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].

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments