0% found this document useful (0 votes)
20 views2 pages

Differentiate Between Readline

The document explains the differences between the readline() and readlines() functions in Python, highlighting that readline() reads a single line while readlines() reads all lines into a list. It also compares two code snippets for reading from a file, noting that the first snippet reads a limited number of bytes without closing the file, while the second uses a context manager to automatically close the file after reading. Additionally, it outlines a programming task for updating employee records in a binary file, including necessary module imports and file handling statements.

Uploaded by

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

Differentiate Between Readline

The document explains the differences between the readline() and readlines() functions in Python, highlighting that readline() reads a single line while readlines() reads all lines into a list. It also compares two code snippets for reading from a file, noting that the first snippet reads a limited number of bytes without closing the file, while the second uses a context manager to automatically close the file after reading. Additionally, it outlines a programming task for updating employee records in a binary file, including necessary module imports and file handling statements.

Uploaded by

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

1. Differentiate between readline() and readlines().

2. What is the difference between the following set of statements


(a) and (b):
(a)
P = open("practice.txt", "r")

P.read(10)

(b)
with open("practice.txt", "r") as P:

x = P.read()

Answer
The code given in (a) will open file "practice.txt" in read mode
and will read 10 bytes from it. Also, it will not close the file
explicitly. On the other hand, the code given in (b) will open file
"practice.txt" in read mode and will read the entire content of
the file. Furthermore, it will automatically close the file after
executing the code due to the with clause.
3. Aman is a Python programmer. He has written a code and created a
binary file record.dat with employeeid, ename and salary. The file contains
10 records. He now has to update a record based on the employee id
entered by the user and update the salary. The updated record is then to
be written in the file temp.dat. The records which are not to be updated
also have to be written to the file temp.dat. If the employee id is not
found, an appropriate message should to be displayed. As a Python
expert, help him to complete the following code based on the requirement
given above:
(i) Which module should be imported in the program? (Statement 1)
(ii) Write the correct statement required to open a temporary file named
temp.dat. (Statement 2)
(iii) Which statement should Aman fill in Statement 3 to read the data
from the binary file, record.dat and in Statement 4 to write the updated
data in the file, temp.dat?
1. Given a binary file “emp.dat” has structure (Emp_id, Emp_name, Emp_Salary). Write a function
in Python countsal() in Python that would read contents of the file “emp.dat” and display the
details of those employee whose salary is greater than 20000

You might also like