0% found this document useful (0 votes)
4 views4 pages

Day 28 Python Answers - 60399344 - 2025 - 05 - 13 - 08 - 52

The document covers file handling in Python, including types of files, opening modes, reading and writing methods, and the use of the `with` statement for resource management. It also explains the differences between `read()`, `readline()`, and `readlines()` methods, as well as the `seek()` and `tell()` methods. Additionally, it provides programming exercises related to reading CSV files and appending text to existing files.

Uploaded by

shalugarg8679
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Day 28 Python Answers - 60399344 - 2025 - 05 - 13 - 08 - 52

The document covers file handling in Python, including types of files, opening modes, reading and writing methods, and the use of the `with` statement for resource management. It also explains the differences between `read()`, `readline()`, and `readlines()` methods, as well as the `seek()` and `tell()` methods. Additionally, it provides programming exercises related to reading CSV files and appending text to existing files.

Uploaded by

shalugarg8679
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

HPSC PGT(CS)- Python Subjective Series

Day 28
Syllabus Coverage

File Handling in Python

- Types of files: text, binary, CSV.

- Relative and absolute paths.

- Text files: opening modes (`r`, `r+`, `w`, `w+`, `a`, `a+`), closing files, `with` clause.

- Writing/appending data: `write()`, `writelines()`.

- Reading data: `read()`, `readline()`, `readlines()`.

- `seek` and `tell` methods, data manipulation.

- Binary files: opening modes (`rb`, `rb+`, `wb`, `wb+`, `ab`, `ab+`), closing files.

- Pickle module: `dump()`, `load()`.

- Operations: read, write, search, append, update.

- CSV files: `csv` module, `csv.writer()`, `csv.reader()`.

Question of the day:


1. Why is it important to close a file after performing operations? How
can the with statement help? 3 M
2. What are the differences between read(), readline(),
and readlines() methods? 4 M
3. Explain the use of seek() and tell() methods in file handling. 4M

1. Why is it important to close a file after performing operations? How can


the with statement help? (3 Marks)

Answer:

• Importance of closing a file:

o To free up system resources.

o To ensure all data is properly written (prevents data loss).

o To avoid file corruption or locking issues (other programs may not access it if
left open).

• Role of with statement:


o Automatically closes the file after the block of code executes, even if an
exception occurs.

o Ensures proper resource management without explicitly calling close().

Example:

with open("example.txt", "r") as file:

data = file.read()

# File is automatically closed here

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.

Reads a single line at When processing large files line by line


readline() String
a time. (memory efficient).

Reads all lines into a List of When needing to manipulate lines as a


readlines()
list. strings list.

Example:

file = open("example.txt", "r")

print(file.read()) # Entire content as a single string

print(file.readline()) # First line

print(file.readlines()) # All lines as a list

file.close()

3. Explain the use of seek() and tell() methods in file handling. (4 Marks)

Answer:

• tell():

o Returns the current position (in bytes) of the file pointer.

o Helps track where the next read/write operation will occur.

• seek(offset, whence):
o Moves the file pointer to a specified position.

o offset: Number of bytes to move.

o whence: Reference point (0=start, 1=current, 2=end).

Example:

file = open("example.txt", "r")

print(file.tell()) # Output: 0 (start of file)

file.seek(10) # Move to 10th byte

print(file.tell()) # Output: 10

file.close()

Program of the day:


1. Write a Python program to read a CSV file and display its contents row
by row. 5M
2. Write a Python program to append text to an existing file and then
display the updated content. 5M
1.

Sample CSV File (students.csv):

RollNo,Name,Mark

101,Arjun,85

102,Rajeev,92

103,Brinda,78

Python Program:

import csv

def display_csv(file_name):

print("Contents of CSV file:")

with open(file_name, 'r') as file:

reader = csv.reader(file)

for row in reader:

print(row)
# Test the function

display_csv("students.csv")

Expected Output:

Contents of CSV file:

['RollNo', 'Name', 'Mark']

['101', 'Arjun', '85']

['102', 'Rajeev', '92']

['103', 'Brinda', '78']

2.

Sample Text File (notes.txt):

Hello, this is line 1.

This is line 2.

Python Program:

def append_and_display(file_name, text_to_append):

# Append text

with open(file_name, 'a') as file:

file.write("\n" + text_to_append)

# Display updated content

print("Updated file content:")

with open(file_name, 'r') as file:

for line in file:

print(line.strip())

# Test the function

append_and_display("notes.txt", "This is an appended line.")

Expected Output:

Updated file content:

Hello, this is line 1.

This is line 2.

This is an appended line.

You might also like