Filehandling Worksheet6
Filehandling Worksheet6
WORKSHEET – 6
1 Give two differences between Text file and Binary File.
ANS
ANS
6. Write a function to display all the words starting with the letter “M” in reverse order from
the file ‘Story.txt’
ANS
7. Write a function in python to count the number of lower case letters in each line from a file
named as ‘Story.txt’
ANS
8. What is the difference between write() and writelines() methods in TextFiles?
ANS
9. Write a function AMCount() in Python, which should read each character of a text file
STORY.TXT, should count and display the occurence of alphabets A and M (including small
cases a and m too).
ANS
11.
What is Serialisation or Pickling and De-Serialisation or Unpickilng in Binary files?
ANS
12.
What do you mean by dump() and load() methods? Give their syntax.
ANS
13. Consider the following Python code and complete the missing statement:
import pickle
myfile = open("test.dat","wb")
d={1:’SUN’,2:’MON’,3:’TUE’}
#statement to store dictionary d into the file
myfile.close()
ANS
14 Consider the following Python code and complete the missing statement:
import pickle
myfile = open("test.dat","rb")
d= #statement to load dictionary data from file to “d‟
print(d)
myfile.close()
ANS
15.
For the above given binary file data ,Fill in the blanks for the condition and appropriate
jump statement for the below code ,to display the total salary as 14000
import pickle
def RECSHOW():
emp=[]
f = open('employee.dat','rb') ;tsal=0
try:
emp = pickle.load(f) # loading data in emp list
for e in emp:
if ___________:
_________________
tsal+=e[2]
except EOFError:
break
print(‘Total salary is’,tsal)
f.close()
RECSHOW()
16. Write a python function to read a binary file to search for a particular rollno and remove that
record from the file if the file name is test.dat and the details stored are in the form of list as
[rollno,name,marks]
ANS
17. CSV stands for ______________
ANS
18. object is used to read data from csv file and __________object is used to write into
csv file.
ANS
Write Python function COMPEMP() to read the content of file ,emp.csv and display only those
records with a delimiter as ‘:’,whose salary is 3000 and above
ANS
20. Consider the following CSV file (emp.csv):
1,Harsha,4500
2,Sunny,5000
3,Sandy,3000
4,Jerin,1500
5,Jia,3200
Write a Python function DISPNAMES() to read the content of file emp.csv and display the
employee records whose names begin with “J/j‟ .Also show number of employee with first
letter “J‟ out of total records.
Output should be:
4,Jerin,1500
5,Jia,32002,
Number of names starting with ‘J’ are 2/5
ANS