0% found this document useful (0 votes)
18 views7 pages

Filehandling Worksheet6

The document discusses various file handling operations in Python like reading, writing, opening and closing files in different modes. It also discusses serialization and deserialization of binary files, CSV file handling and contains questions related to these topics.

Uploaded by

Sangeetha MD
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)
18 views7 pages

Filehandling Worksheet6

The document discusses various file handling operations in Python like reading, writing, opening and closing files in different modes. It also discusses serialization and deserialization of binary files, CSV file handling and contains questions related to these topics.

Uploaded by

Sangeetha MD
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/ 7

FILE HANDLING

WORKSHEET – 6
1 Give two differences between Text file and Binary File.
ANS

2 What is the different in file opening mode “a” and “w” ?


ANS

3. What is the difference between readline() and readlines() ?


ANS

4. What is the purpose of using flush() in file handling operations ?

ANS

5. What is the advantage of opening file using “with‟ keyword?


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).

Example: If the file content is as follows:


Updated information As simplified by official websites.
The AMCount() function should display the output as: A or a:4 M or m :2

ANS

10 What are seek() and tell() functions? Give their syntax.


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

19. Consider the following CSV file (emp.csv):


1,Harsha,4500
2,Sunny,5000
3,Sandy,3000
4,Merill,1500
5,Jia,3200

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

You might also like