Below is a FULL beginner-friendly Lesson 11, explaining File Handling in Python with clear explanations, examples, and outputs.
File handling is important because programs often store and read data from files.
๐ Lesson 11: File Handling in Python
๐น 1. What is File Handling?
File handling allows Python programs to:
โ Read data from files
โ Write data to files
โ Store information permanently
Example uses:
- Saving user data
- Logging system activity
- Reading configuration files
๐น 2. Opening a File
Python uses the open() function.
Syntax
file = open("filename", "mode")
File Modes
| Mode | Meaning |
|---|---|
"r" | Read file |
"w" | Write file (overwrites) |
"a" | Append to file |
"x" | Create new file |
"b" | Binary mode |
๐น 3. Reading a File
Example 1: Read Entire File
Assume file data.txt contains:
Sameer
Ayesha
Saddam
Python Code
file = open("data.txt", "r")
content = file.read()
print(content)
file.close()
Output
Sameer
Ayesha
Saddam
๐ Explanation
"r"mode reads fileread()reads entire contentclose()closes file
๐น 4. Reading File Line by Line
Example 2
file = open("data.txt", "r")
for line in file:
print(line)
file.close()
Output
Sameer
Ayesha
Saddam
๐ Explanation
Loop reads each line one by one.
๐น 5. Writing to a File
Example 3
file = open("data.txt", "w")
file.write("Sameer\n")
file.write("Ayesha\n")
file.close()
๐ Explanation
"w"mode overwrites existing file\ncreates a new line
File Content After Running
Sameer
Ayesha
๐น 6. Appending to a File
Example 4
file = open("data.txt", "a")
file.write("Saddam\n")
file.close()
File Content
Sameer
Ayesha
Saddam
๐ Explanation"a" mode adds content without deleting old data
๐น 7. Reading One Line
Example 5
file = open("data.txt", "r")
line = file.readline()
print(line)
file.close()
Output
Sameer
๐ Explanationreadline() reads one line only
๐น 8. Reading All Lines as List
Example 6
file = open("data.txt", "r")
lines = file.readlines()
print(lines)
file.close()
Output
['Sameer\n', 'Ayesha\n', 'Saddam\n']
๐ Explanation
Each line becomes a list element.
๐น 9. Using with open() (Best Practice)
Python automatically closes file.
Example 7
with open("data.txt", "r") as file:
content = file.read()
print(content)
Output
Sameer
Ayesha
Saddam
๐ Explanation
โ No need to call close()
โ Cleaner and safer code
๐น 10. Creating a New File
Example 8
file = open("students.txt", "x")
file.write("Chandini")
file.close()
๐ Explanation
"x"creates a new file- Error occurs if file already exists
๐น 11. Real-Life Example
Student Record System
name = input("Enter student name: ")
with open("students.txt", "a") as file:
file.write(name + "\n")
print("Student saved successfully")
Sample Output
Enter student name: Gousya
Student saved successfully
๐น 12. Common File Methods
| Method | Purpose |
|---|---|
read() | Reads entire file |
readline() | Reads one line |
readlines() | Reads all lines |
write() | Writes data |
close() | Closes file |
๐น 13. Common Mistakes
โ Forgetting to close file
โ Using "w" accidentally (deletes content)
โ File not found error

Leave a Reply