I created a file CRUD application that will create, read, update or delete any file. I created it using Python.
import os import time # Reader class to read the file class Reader: def __init__(self, fileToRead): self.file = fileToRead def readFile(self): try: with open(self.file, "r") as file: data = file.read() print(f"Data at {self.file} is below: \n") print(data) print("\nPress any key to return to menu") input() # If user typed file not present in the directory. except FileNotFoundError: print("Your file not found in the directory. Please check name of file and try Again.") print("Press any key to return to menu") input() # Deleter class to delete the file class Deleter: def __init__(self, fileToDelete): self.file = fileToDelete def deleteFile(self): try: action = input(f"Are you sure that you want to remove {self.file}? Yes/No: ") if action in ["Yes", "yes", "y", "Y"]: os.remove(self.file) print(f"\nYour file {self.file} Deleted Successfully") print("\nPress any key to return to menu") input() # If user typed file not present in the directory. except FileNotFoundError: print("Your file not found in the directory. Please check name of file and try Again.") print("\nPress any key to return to menu") input() # Modifier class to modify a file class Modifier: def __init__(self, fileToModifiy): self.file = fileToModifiy def modifyFile(self): try: data = input(f"Enter the data you want to write into {self.file}") with open(self.file, "r") as file: previousData = file.read() with open(self.file, "w") as file: file.write(data) print("\nData written successfully") print("\nPress any key to return to menu") input() # If user typed file not present in the directory. except FileNotFoundError: print("Your file not found in the directory. Please check name of file and try Again.") print("\nPress any key to return to menu") input() class Creater: def __init__(self, fileName): self.name = fileName def createFile(self, data): with open(self.name, "w") as file: file.write(data) print("File Created Successfully") print(f"Data has been written to {self.name} Successfully") print("\nPress any key to return to menu") input() # Main file class for running functions and take input from user and give to their specific classes class File: def readFile(self): self.file = input("Enter the path of the file you want to read: ") return self.file def deleteFile(self): self.file = input("Enter the name of the file you want to delete: ") return self.file def modifyFile(self): self.file = input("Enter the path of file you want to modify: ") return self.file def createFile(self): self.name = input("Enter the name of file you want to create. (With Extension): ") return self.name def dataInFile(self): self.data = input("Enter data you want to put in file: ") return self.data # Function for clear the Console def clearConsole(): # If os is Windows command = "clear" # if os is other than windows if os.name in ("nt", "dos"): command = "cls" # Run the command to clear console os.system(command) if __name__ == "__main__": file = File() # Running infinite loop in order to create a menu while True: clearConsole() # Printing the menu print('''\n ███████╗██╗██╗░░░░░███████╗ ░█████╗░██╗░░░██╗██████╗░██████╗░ ██╔════╝██║██║░░░░░██╔════╝ ██╔══██╗██║░░░██║██╔══██╗██╔══██╗ █████╗░░██║██║░░░░░█████╗░░ ██║░░╚═╝██║░░░██║██████╔╝██║░░██║ ██╔══╝░░██║██║░░░░░██╔══╝░░ ██║░░██╗██║░░░██║██╔══██╗██║░░██║ ██║░░░░░██║███████╗███████╗ ╚█████╔╝╚██████╔╝██║░░██║██████╔╝ By: Hussnain Ahmad\n\nEnter choice from below:\n1. Create A File\n2. Read A File\n3. Delete A File\n4. Modify A File\n5. Exit\n''') # Taking user choice userChoice = input("Enter your choice: ") # comparing user choice with error handling. try: if int(userChoice) == 1: clearConsole() fileToCreate = file.createFile() fileData = file.dataInFile() creater = Creater(fileToCreate) creater.createFile(fileData) elif int(userChoice) == 2: clearConsole() fileToRead = file.readFile() reader = Reader(fileToRead) reader.readFile() elif int(userChoice) == 3: clearConsole() fileToDelete = file.deleteFile() deleter = Deleter(fileToDelete) deleter.deleteFile() elif int(userChoice) == 4: clearConsole() fileToModify = file.modifyFile() modifier = Modifier(fileToModify) modifier.modifyFile() elif int(userChoice) == 5: print("Thanks for using this software. We hope to see you again.") time.sleep(3) break else: clearConsole() print("Wrong Choice. Please Try Again!") except: clearConsole() print("Invalid Choice")
Demo:
Comments
Post a Comment