MadiisAttendance
Python programming complete guide 2 min read 387 words 38 sentences uni

Python in 7 Days – Day 5: File Handling, Error Handling & Building a Note-Taker App

M Usman May 25, 2026
18 0 0 score
0%
Your Progress
0/13 sections
Reading Speed
0
words/min
Time Spent
00:00
Table of Contents

Quick Stats
Words: 387
Est. time: 2 min
Readability: Easy

Word Cloud
magot overprovidentness dynastinae gavelet hopbind bollocks paauw guimpe coheaded bipack exaggeratory pretympanic pilgrimess trustmonger CINEMATOGRAPHER

1. Reading a Text File and Counting Words

with open('text_file.txt', 'r') as f:

Explanation

  • open() is used to open a file.
  • 'text_file.txt' is the file name.
  • 'r' means read mode.
  • with automatically closes the file after use.
  • as f stores the file object in variable f.

content = f.read()

Explanation

  • read() reads all text from the file.
  • The text is stored in variable content.

Example:

If the file contains:

Hello world Python is easy

Then:

content = "Hello world Python is easy"

words = content.split()

Explanation

  • split() breaks text into separate words.
  • By default, it splits where spaces occur.

Example:

"Hello world Python".split()

becomes:

['Hello', 'world', 'Python']

So words becomes a list of words.


print(f"Number of words: {len(words)}")

Explanation

  • len(words) counts total words in the list.
  • f"" is an f-string used to insert values into text.

Example Output:

Number of words: 5

2. Handling Division by Zero Using try-except

try:

Explanation

  • try is used to test code that may cause an error.

num1 = 10
num2 = 0

Explanation

Two variables are created:

  • num1 = 10
  • num2 = 0

result = num1 / num2

Explanation

This tries to divide:

10÷010 \div 0

Division by zero is not allowed in Python.

So Python raises an error called:

ZeroDivisionError

print(result)

Explanation

  • This would print the result.
  • But it never runs because the error happens before it.

except ZeroDivisionError:

Explanation

  • except catches the error.
  • If a ZeroDivisionError occurs, Python runs this block instead of stopping the program.

print("Error: Division by zero")

Explanation

This prints a custom error message.

Output:

Error: Division by zero

3. Simple Note-Taker Program

with open('notes.txt', 'a') as f:

Explanation

  • Opens the file notes.txt.
  • 'a' means append mode.
  • Append mode adds new data without deleting old data.

note = input("Enter a note: ")

Explanation

  • input() asks the user to type something.
  • The typed text is stored in variable note.

Example:

Enter a note: Study Python today

Then:

note = "Study Python today"

f.write(note + '\n')

Explanation

  • write() saves text into the file.
  • '\n' adds a new line after the note.

So every new note goes on a separate line.

Example file content:

Study Python today
Buy groceries
Complete homework

Final Concepts Used

ConceptPurpose
open()Open a file
'r' modeRead file
'a' modeAppend to file
read()Read file content
split()Separate text into words
len()Count items
tryTest risky code
exceptHandle errors
input()Take user input
write()Save data into file
Discussion (0)
Login to comment
Dictionary

Add New Word

Dictionary Words
My Notes
Highlights
Select text and click highlight to save
My Vocabulary
Quick Quiz
Settings
Reading Analytics
Today's reading: 0 min
Total read time: 0 min
Words learned: 0
Streak: 0 days
AI Summary

Generating summary...