🐍 Lesson 30: Build Your First Python Project – A Command-Line Tools App
Welcome to Lesson 30! Today we’ll build a complete Python mini-project: a Command-Line Tools App. This project combines everything you’ve learned — functions, loops, file handling, error handling, modules, and more. It’s simple, powerful, and a perfect project to add to your portfolio!
💡 Key Benefits of Building a Command-Line Tools App
- Hands-on experience building real Python applications
- Learn how to structure and organize your code efficiently
- Master handling user input and managing program flow
- Practice error handling and debugging
- Enhance your portfolio with a functional CLI project
🛠 1. What We’re Building
A CLI app where users can choose from multiple tools:
- 📁 File Reader
- 🧮 Calculator
- 📝 To-Do List Manager
- 🔍 Search Text in File
Let’s build it step by step.
📦 2. Project Structure
cli_tool/
│── main.py
📦 3. The Full Project Code
import os
def read_file():
filename = input("Enter filename: ")
try:
with open(filename, "r") as file:
print("\n--- File Content ---")
print(file.read())
print("---------------------\n")
except FileNotFoundError:
print("File not found!\n")
def calculator():
try:
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Enter operator (+, -, *, /): ")
if op == "+":
print("Result:", a + b)
elif op == "-":
print("Result:", a - b)
elif op == "*":
print("Result:", a * b)
elif op == "/":
print("Result:", a / b)
else:
print("Invalid operator!")
except Exception as e:
print("Error:", e)
def todo_list():
tasks = []
while True:
print("\n[1] Add Task")
print("[2] View Tasks")
print("[3] Remove Task")
print("[4] Back to Main Menu")
choice = input("Choose option: ")
if choice == "1":
task = input("Enter task: ")
tasks.append(task)
elif choice == "2":
print("\nYour Tasks:")
for i, t in enumerate(tasks, 1):
print(f"{i}. {t}")
elif choice == "3":
num = int(input("Task number to remove: "))
if 1 <= num <= len(tasks):
tasks.pop(num - 1)
else:
print("Invalid number!")
elif choice == "4":
break
else:
print("Invalid option!")
def search_text():
filename = input("Enter filename: ")
keyword = input("Enter word to search: ")
try:
with open(filename, "r") as file:
content = file.read()
if keyword in content:
print("✓ Word found!")
else:
print("✗ Word not found!")
except FileNotFoundError:
print("File not found!")
def main():
while True:
print("\n===== Command-Line Tools App =====")
print("[1] Read File")
print("[2] Calculator")
print("[3] To-Do List")
print("[4] Search Text in File")
print("[5] Exit")
choice = input("Choose option: ")
if choice == "1":
read_file()
elif choice == "2":
calculator()
elif choice == "3":
todo_list()
elif choice == "4":
search_text()
elif choice == "5":
print("Goodbye!")
break
else:
print("Invalid option. Try again!")
if __name__ == "__main__":
main()
📌 4. How to Run the Project
python main.py
🧠 What You Learned
- Building real CLI apps
- Organizing code with functions
- Handling user input
- Error handling
- Working with files
🧪 Practice
- Add a password generator tool.
- Add a currency converter using an API.
- Save to-do tasks permanently into a text file.
- Make a menu animation or loading effect.
❓ Frequently Asked Questions (FAQ)
1. How do I run this CLI app on my computer?
To run the project, navigate to the project folder and use the command: python main.py in your terminal.
2. Can I add more tools to this app?
Absolutely! You can add as many tools as you like by creating new functions and adding them to the main menu.
3. What if I encounter a "File not found" error?
This error occurs when the file you're trying to read doesn't exist. Make sure the file is in the correct directory and its name is correct.
4. Can I use this app in a real-world project?
Yes, this is a great foundation for building command-line applications. You can extend it for tasks like managing logs, sending reports, etc.
Comments
Post a Comment