CodingNic

Files, Errors, and Automation

Files and Errors Challenges

Files, Errors, and Automation 32 min read

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:
text
Learn Python
Build Projects
Keep Going
  • Read the file
  • Print each line

Expected output:

text
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:

text
Today I practiced Python

Challenge 3: CSV Scores

Steps:

  • Create scores.csv
  • Add:
text
name,score
Tom,85
Sara,92
  • Read the file with DictReader
  • Print name and score

Expected output:

text
Tom 85
Sara 92

Challenge 4: JSON Settings

Steps:

  • Create this dictionary:
python
{
    "theme": "dark",
    "language": "English"
}
  • Save it into settings.json
  • Read it back
  • Print both values

Expected output:

text
dark
English

Challenge 5: Safe Number Input

Steps:

  • Ask the user for a number
  • Convert it to int
  • If input is invalid, print:
text
Invalid number

Expected output:

text
Enter number: hello
Invalid number

Mini Projects

Project 1: Backup Tool

Steps:

  • Create a folder named backup
  • Copy notes.txt into 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:
python
["notes.txt", "photo.jpg", "tasks.txt", "music.mp3"]
  • Use a loop
  • Print only .txt files

Expected output:

text
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:
text
Done

Final Challenge

Build a Student File Manager.

Requirements:

  • Store students in students.json

Use this data:

python
[
    {"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 80 or more
  • Handle missing file errors
  • Create a backup copy of the file

Expected output:

text
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

  1. Why use append mode?
  2. Why use try/except?
  3. Why use JSON instead of plain text sometimes?
  4. 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.