Day 28 Python Answers - 60399344 - 2025 - 05 - 13 - 08 - 52
Day 28 Python Answers - 60399344 - 2025 - 05 - 13 - 08 - 52
Day 28
Syllabus Coverage
- Text files: opening modes (`r`, `r+`, `w`, `w+`, `a`, `a+`), closing files, `with` clause.
- Binary files: opening modes (`rb`, `rb+`, `wb`, `wb+`, `ab`, `ab+`), closing files.
Answer:
o To avoid file corruption or locking issues (other programs may not access it if
left open).
Example:
data = file.read()
2. What are the differences between read(), readline(), and readlines() methods? (4 Marks)
Answer:
Return
Method Description Use Case
Type
Reads the entire file When the file is small and needs full
read() String
content. content.
Example:
file.close()
3. Explain the use of seek() and tell() methods in file handling. (4 Marks)
Answer:
• tell():
• seek(offset, whence):
o Moves the file pointer to a specified position.
Example:
print(file.tell()) # Output: 10
file.close()
RollNo,Name,Mark
101,Arjun,85
102,Rajeev,92
103,Brinda,78
Python Program:
import csv
def display_csv(file_name):
reader = csv.reader(file)
print(row)
# Test the function
display_csv("students.csv")
Expected Output:
2.
This is line 2.
Python Program:
# Append text
file.write("\n" + text_to_append)
print(line.strip())
Expected Output:
This is line 2.