๐Ÿ“˜ Lesson 11: File Handling in Python

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

ModeMeaning
"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 file
  • read() reads entire content
  • close() 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
  • \n creates 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

๐Ÿ“Œ Explanation
readline() 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

MethodPurpose
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



Comments

Leave a Reply

Your email address will not be published. Required fields are marked *