CS Data File Handling SLW
CS Data File Handling SLW
WORKSHEET
DATA FILE HANDLING
1 Give one difference between Text file and Binary File
2 Write a Python statement to open a text file “DATA.TXT” so that new contents can
be written on it.
3 Write a Python statement to open a text file “DATA.TXT” so that new content can be
added to the end of file
4 Write a Python statement to open a text file “DATA.TXT” so that existing contents
can be read from file.
6 What is the significance of adding „+‟ with file opening mode, with context to „r+‟ ?
1|Page
1 Considering the content stored in file “CORONA.TXT”
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
Complete the missing statement using „for‟ loop to print all the lines of file
f = open(“CORONA.TXT”)
for :
print( )
14 Write a function in python to count the number of lines in “POEM.txt” begins from
Upper case character.
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
Output should be: Lines starting from Capital letters: 4
15 Write a function in python to read lines from file “POEM.txt” and count how many
times the word “Corona” exists in 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
Output should be: Number of time word Corona occurs : 4
2|Page
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
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
3|Page
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.
2 Write a Python statement to open a text file “DATA.TXT” in binary mode so that
new contents can be written on it.
3 Write a Python statement to open a text file “DATA.TXT” in binary mode so that
new content can be added to the end of file
4 Write a Python statement to open a text file “DATA.TXT” in binary mode so that
existing contents can be read from file.
6 Consider the following Python code, and fill in the blank to complete the
program
f=open("India.txt","wb")
str="India is my country"
f. (str.encode()) # statement to store the str in file
f.close()
4|Page
10 Consider a binary file which stores Name of employee, where each name
occupies 20 bytes (length of each name) in file irrespective of actual
characters. Now you have to write code to access the first name, 5th name and
last name.
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()
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
5|Page
15 is the process of converting structures to byte stream before writing to
file.
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()
6|Page