Python file handling is one of the most scoring topics in ECET.
Questions usually come from opening files, reading files, writing files, file modes, and close() function.
This topic is extremely easy once you understand the functions and modes clearly.
📘 Concept Notes – Python File Handling
✅ What is File Handling?
File handling allows Python to store, retrieve, update, or delete data in files stored on your computer.
It is used when you want to keep data permanent.
⚙️ Opening a File (open)
To open a file, Python uses the open() function.
🔥 Syntax:
open("filename", "mode")📌 Common Modes
| Mode | Meaning |
|---|---|
"r" | Read mode (file must exist) |
"w" | Write mode (overwrites file) |
"a" | Append mode (adds data at end) |
"r+" | Read + Write |
"w+" | Write + Read |
"a+" | Append + Read |
Example
file = open("data.txt", "r")📖 Reading a File
Python provides 3 common ways to read:
1️⃣ read() → Reads entire file
file = open("data.txt", "r")
content = file.read()
print(content)
file.close()2️⃣ readline() → Reads single line
line = file.readline()3️⃣ readlines() → Reads all lines as list
lines = file.readlines()✍️ Writing to a File
To write, file must be opened using "w" or "a" mode.
1️⃣ write()
file = open("output.txt", "w")
file.write("Hello ECET Students!")
file.close()2️⃣ writelines() (write list of strings)
file.writelines(["Line1\n", "Line2\n"])🔒 Closing the File
Always close file after work:
file.close()Closing ensures data is saved properly.
🧾 Using with open() (Recommended)
Python automatically closes the file.
with open("sample.txt", "r") as f:
data = f.read()
print(data)📐 Formula (Important for MCQs)
Number of bytes read using read(n) is:
![]()
If file length is
, and you read
bytes:
![]()
🔟 10 Expected MCQs – ECET 2026
Q1. Which function is used to open a file in Python?
A) openfile()
B) file()
C) open()
D) read()
Q2. Which mode is used for writing a file?
A) r
B) w
C) a
D) r+
Q3. Which method reads entire file content?
A) read()
B) readline()
C) readlines()
D) get()
Q4. write() method is used with:
A) Read mode
B) Append or Write mode
C) Only Binary mode
D) None
Q5. Which mode overwrites an existing file?
A) w
B) a
C) r
D) r+
Q6. readlines() returns:
A) String
B) Integer
C) List of strings
D) Dictionary
Q7. Using with open() automatically:
A) deletes file
B) closes file
C) overwrites file
D) appends file
Q8. If file size is
bytes and you read
bytes, remaining bytes = ?
A) 30
B) 10
C) 40
D) 50
(Use formula:
)
Q9. Which method reads one line at a time?
A) read()
B) readline()
C) readlines()
D) readfile()
Q10. append mode "a":
A) overwrites file
B) reads file
C) adds data at end
D) deletes file
✅ Answer Key
| Q.No | Answer |
|---|---|
| Q1 | C |
| Q2 | B |
| Q3 | A |
| Q4 | B |
| Q5 | A |
| Q6 | C |
| Q7 | B |
| Q8 | C |
| Q9 | B |
| Q10 | C |
🧠 Explanations
- Q1 → C: open() is the correct built-in function.
- Q2 → B: “w” is write mode.
- Q3 → A: read() reads entire file.
- Q4 → B: Writing is allowed only in w, a modes.
- Q5 → A: “w” clears old data and overwrites.
- Q6 → C: readlines() returns list.
- Q7 → B: with automatically closes file.
- Q8 → C:
. - Q9 → B: readline() reads single line.
- Q10 → C: “a” adds new content at end.
🎯 Why This Topic Is Important for ECET
- Very easy & high-scoring.
- Only few functions to remember.
- MCQs are often direct from modes and functions.
- Python file handling is useful in real programming too.
📲 Join our ECET WhatsApp Community
👉 Join WhatsApp Group:
https://chat.whatsapp.com/GniYuv3CYVDKjPWEN086X9
Get daily:
✔ ECET Notes
✔ MCQs
✔ Practice Papers
✔ Video Explanations

