Files and Errors Challenges
Files and Errors Challenges
Great job reaching this stage.
You have learned how to:
- read files
- write files
- append text
- use CSV files
- use JSON files
- handle errors
- work with folders
- copy and move files
- automate tasks
Now it is time to practice everything together.
Quick Challenges
Try these on your own.
Challenge 1: Notes Reader
Steps:
- Create a file named
notes.txt - Add these lines:
Learn Python
Build Projects
Keep Going
- Read the file
- Print each line
Expected output:
Learn Python
Build Projects
Keep Going
Challenge 2: Journal Saver
Steps:
- Ask the user for one note
- Save it into
journal.txt - Use append mode
- Add a new line after the note
Expected file example:
Today I practiced Python
Challenge 3: CSV Scores
Steps:
- Create
scores.csv - Add:
name,score
Tom,85
Sara,92
- Read the file with
DictReader - Print name and score
Expected output:
Tom 85
Sara 92
Challenge 4: JSON Settings
Steps:
- Create this dictionary:
{
"theme": "dark",
"language": "English"
}
- Save it into
settings.json - Read it back
- Print both values
Expected output:
dark
English
Challenge 5: Safe Number Input
Steps:
- Ask the user for a number
- Convert it to
int - If input is invalid, print:
Invalid number
Expected output:
Enter number: hello
Invalid number
Mini Projects
Project 1: Backup Tool
Steps:
- Create a folder named
backup - Copy
notes.txtinto it - Rename the copied file to
notes_old.txt
Expected result:
A backup file inside the folder.
Project 2: File Finder
Steps:
- Create this list:
["notes.txt", "photo.jpg", "tasks.txt", "music.mp3"]
- Use a loop
- Print only
.txtfiles
Expected output:
notes.txt
tasks.txt
Project 3: Safe Divider
Steps:
- Ask for two numbers
- Divide first by second
- Handle:
- invalid input
- divide by zero
- Always print:
Done
Final Challenge
Build a Student File Manager.
Requirements:
- Store students in
students.json
Use this data:
[
{"name": "Tom", "score": 85},
{"name": "Sara", "score": 92}
]
Tasks:
- Save data to JSON
- Load data from JSON
- Print all students
- Print only students with score
80or more - Handle missing file errors
- Create a backup copy of the file
Expected output:
Tom 85
Sara 92
Tom
Sara
Backup created
Real World Use Case
Real apps use files and error handling to save data, recover from mistakes, and automate daily tasks safely.
Quiz
- Why use append mode?
- Why use
try/except? - Why use JSON instead of plain text sometimes?
- What does automation save?
Assignment
Choose one challenge and improve it with better messages, extra features, or cleaner output.
Summary
You practiced real Python file handling, error handling, and automation skills in one lesson.