File Handling Worksheet-1
File Handling Worksheet-1
16 Write a function in python to read lines from file “POEM.txt” and display all those
words, which has two characters in it.
For e.g. if the content of file is
O Corona O Corona
Jaldi se tum Go na
Social Distancing ka palan karona
sabse 1 meter ki duri rakhona
Lockdown me ghar me ho to
online padhai karona
O Corona O Corona
Jaldi se tum Go na
Output should be : se Go na ka ki me me ho to se Go na
17 Write a function COUNT() in Python to read contents from file “REPEATED.TXT”, to
count and display the occurrence of the word “Catholic” or “mother”.
For example:
If the content of the file is “Nory was a Catholic because her mother was a Catholic , and
Nory‟s mother was a Catholic because her father was a Catholic , and her father was a
Catholic because his mother was a Catholic , or had been
The function should display:
Count of Catholic, mother is 9
18 Write a function dispS() in Python to read from text file “POEM.TXT” and display
those lines which starts with “S”
For example:
If the content of the file is “
O Corona O Corona
Jaldi se tum Go na
Social Distancing ka palan karona
Sabse 1 meter ki duri rakhona
Lockdown me ghar me ho to
online padhai karona
O Corona O Corona
Jaldi se tum Go na
The function should display:
Social Distancing ka palan karona
Sabse 1 meter ki duri rakhona
19 Write a function COUNTSIZE() in Python to read the file “POEM.TXT” and display
size of file. For e.g. if the content of file is :
O Corona O Corona
Jaldi se tum Go na
Social Distancing ka palan karona
sabse 1 meter ki duri rakhona
Lockdown me ghar me ho to
online padhai karona
O Corona O Corona
Jaldi se tum Go na
The function should display
Size of file is 184
20 Write a python function ATOEDISP() for each requirement in Python to read the file
“NEWS.TXT” and
(I) Display “E” in place of all the occurrence of “A” in the word COMPUTER.
(II) Display “E” in place of all the occurrence of “A”:
I SELL COMPUTARS. I HAVE A COMPUTAR. I NEED A COMPUTAR. I WANT A
COMPUTAR. I USE THAT COMPUTAR. MY COMPUTAR CRASHED.
The function should display
(I) I SELL COMPUTERS. I HAVE A COMPUTER. I NEED A COMPUTER. I WANT A
COMPUTER. I USE THAT COMPTUER. MY COMPUTER CRASHED.
(II) I SELL COMPUTERS. I HEVE E COMPUTER. I NEED E COMPUTER. I WENT E
COMPUTER. I USE THET COMPTUER. MY COMPUTER CRESHED.
f = open("Emp.txt","rb")
s= #code to get first record
print(s.decode())
# code to position at 5th record
s = f.read(size)
print(s.decode())
# code to position at last record
s = f.read(20)
print(s.decode())
f.close()
11 Write a Python statement to reposition the read pointer to 20 bytes back
from the current position.
f = open("Emp.txt","rb")
f.read(20)
f.read(20)
f.read(20)
f. # reposition read pointer to previous record
f.close()
12 Write a function RECCOUNT() to read the content of binary file „NAMES.DAT‟
and display number of records ( each name occupies 20 bytes in file ) in it.
For. e.g. if the content of file is:
SACHIN
AMIT
AMAN
SUSHIL
DEEPAK
HARI SHANKER
Function should display
Total Records are 6
13 Write a function SCOUNT() to read the content of binary file „NAMES.DAT‟ and
display number of records (each name occupies 20 bytes in file ) where name
begins from „S‟ in it.
For. e.g. if the content of file is:
SACHIN
AMIT
AMAN
SUSHIL
DEEPAK
HARI SHANKER
Function should display
Total Names beginning from „S‟ are 2
14 To read and write collections like LIST, DICTIONARIES Python provides a module
called
15 is the process of converting structures to byte stream before writing to
file.
16 is the process of converting byte stream to original structure.
17 Pickling is done by the function
18 Unpickling is done by the function
19 Consider the following Python code and complete the missing statement:
import pickle
myfile = open("test.dat","wb")
d={1:100,2:200,3:300}
#statement to store dictionary d in file
myfile.close()
20 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()
21 Python‟s standard streams are , ,
22 Python‟s standard streams are available in module
23 From the given path identify the type of each:
(i) C:\mydata\web\resources\img.jpg
(ii) ..\web\data.conf
24 Consider the following Binary file „Emp.txt‟, Write a function RECSHOW() to
display only those records who are earning more than 7000
Write Python function DISPEMP() to read the content of file emp.csv and display
only those records where salary is 4000 or above
29 Consider the following CSV file (emp.csv):
1,Peter,3500
2,Scott,4000
3,Harry,5000
4,Michael,2500
5,Sam,4200
Write a Python function DISPEMP() to read the content of file emp.csv and count
how many employee are earning less than 5000
30 Consider the following CSV file (emp.csv):
1,Peter,3500
2,Scott,4000
3,Harry,5000
4,Michael,2500
5,Sam,4200
Write a Python function SNAMES() to read the content of file emp.csv and display
the employee record whose name begins from „S‟ also show no. of employee with
first letter „S‟ out of total record.
Output should be:
2,Scott,4000
5,Sam,4200
Number of „S‟ names are 2/5
if row[1][0].lower()=='s':
print(row[0],',',row[1],',',row[2])
count_s+=1
count_rec+=1
print("Number of 'S' names are ",count_s,"/",count_rec)
31 Write a python function CSVCOPY() to take sourcefile, targetfile as parameter
and create a targetfile and copy the contents of sourcefile to targetfile