A menu-driven Command-Line Interface (CLI) application built in Python that lets users create, read, and delete files directly from the terminal — with live directory listing on every action.
File: file_project.py
A while True loop keeps the program running until the user chooses to quit. At each iteration the user picks an operation from a numbered menu, and the corresponding function is called.
Menu options:
**************************
Press '1' to create a file
Press '2' to read a file
Press '3' to delete a file
Press '4' to quit the program
**************************
| Tool / Module | Purpose |
|---|---|
| Python 3 | Core programming language |
pathlib.Path |
Object-oriented path and file operations |
os.remove() |
Deleting files from the filesystem |
open() built-in |
Reading and writing file content |
Path('')creates aPathobject pointing to the current working directory.path.rglob('*')recursively walks every file and folder inside a directory — much cleaner than nestedos.walk()calls.p.exists()checks whether a path (file or directory) exists on disk before acting on it.p.is_file()narrows the check down to regular files, guarding against accidentally reading a directory name.
path = Path('')
items = list(path.rglob('*')) # recursive listing of all files & folders
p = Path(name)
if not p.exists(): # safe guard before creating
...- Opening a file with
open(p, 'w')creates it (or overwrites it) for writing;open(p, 'r')opens it for reading. - Using the
withstatement (context manager) ensures the file is automatically closed even if an error occurs — no need for a manualf.close().
with open(p, 'w') as fs:
fs.write(data) # write user input to the file
with open(p, 'r') as fs:
data = fs.read() # read the full file content at onceos.remove(name)permanently removes the file at the given path.- Always verify the file exists and is a regular file before calling
remove()to avoid unexpected errors.
import os
os.remove(name)- A
while Trueloop combined with abreakstatement creates an infinite menu that exits only when the user explicitly chooses to quit. if / elif / elsechains map each numeric choice to its handler function.
while True:
check = int(input("Enter your response: "))
if check == 1:
createfile()
elif check == 2:
readfile()
elif check == 3:
deletefile()
elif check == 4:
break # exit the loop cleanly
else:
print(f"{check} is a/an invalid.")enumerate(items)pairs each item with a zero-based index, making it easy to print a numbered list without a manual counter variable.
for i, item in enumerate(items):
print(f"{i+1}: {item}") # 1-based numbering for readability- Wrapping risky operations (file I/O, user input conversion) in
try / except Exception as errprevents the program from crashing unexpectedly. - Catching
Exceptionat the top level surfaces helpful error messages while keeping the program running.
try:
check = int(input("Enter your response: "))
except Exception as error:
print(f"Please enter a valid number [{error}]")- Python f-strings (
f"...") allow variables and expressions to be embedded directly inside string literals — more readable than+concatenation or.format().
print(f"{i+1}: {items}")
print(f"An error occured as {err}")- Each operation (
readfileandfolder,createfile,readfile,deletefile) is encapsulated in its own function — keeping the main loop short and each concern isolated. readfileandfolder()is called at the start of every operation so the user always sees the current state of the directory.
-
Make sure Python 3 is installed on your system.
-
Clone or download this repository, then run:
python file_project.py- Follow the on-screen numbered menu to create, read, or delete files.
| Feature | Supported |
|---|---|
| Recursive directory listing | ✅ |
| Create a file with custom content | ✅ |
| Read and display file content | ✅ |
| Delete a file | ✅ |
| Prevent overwriting existing files | ✅ |
| Error handling & input validation | ✅ |