Differentiate Between Readline
Differentiate Between Readline
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