Worksheet (XII)
CH- FILE HANDLING
Subject:-Computer Science
Q. Write a definition of a function that takes input a dictionary as per following structure and
display only those elements whose salaries are > 56000
Dictionary structure:
{1:[‘john’,98000],2:[‘smith’,58000],……}
Q. Write a function in Python that counts the number of “The” or “To” words present in a text
file “[Link]”.If the “[Link]”.
Q. Write a function ETCount() in Python, which should read each character of a text file
“[Link]” and then count and display the count of occurrence of alphabets E and T
individually (including small cases e and t too).
Q. Write a method COUNTLINES() in Python to read lines from text file ‘[Link]’ and display
the lines which are not starting with any vowel.
Q. Aman is a Python programmer. He has written a code and created a binary file [Link] 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 [Link]. The records which are not to be updated also have to be written to the file
[Link]. 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
import _______ #Statement 1
def update_data():
rec={}
fin=open("[Link]","rb")
fout=open("_____________") #Statement 2
found=False
eid=int(input("Enter employee id to update their salary :: "))
while True:
try:
rec=______________ #Statement 3
if rec["Employee id"]==eid:
found=True
rec["Salary"]=int(input("Enter new salary :: "))
pickle.____________ #Statement 4
else: [Link](rec,fout)
except:
break
if found==True:
print("The salary of employee id ",eid," has been updated.")
else:
print("No employee with such id is not found")
[Link]()
[Link]()
(i) Which module should be imported in the program? (Statement 1)
(ii) Write the correct statement required to open a temporary file named [Link].
(Statement 2)
(iii) Which statement should Aman fill in Statement 3 to read the data from the binary file,
[Link] and in Statement 4 to write the updated data in the file, [Link]?
Q (A)Write a Program in Python that defines and calls the following user defined functions:
(i) add() – To accept and add data of teachers to a CSV file ‘[Link]’. Each record
consists of a list with field elements as tid, tname and tsal to store teacher id, teacher
name and salary respectively using while loop..
(ii) search()- To display and count the records of the teachers whose salary is more
than 15000 from csv file [Link]
(B) Differentiate between text file and csv file.
Q. A binary file “[Link]” has structure [cityno, cityname, pincode, grade].
i. Write a user defined function CreateFile() to input data for a record and
add to [Link].
ii. Write a function CountRec() in Python which accepts the city name as
parameter and count and return number of pincode which is given by user,
are stored in the binary file “[Link]”