Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views5 pages

Data File Handling Class 12 Worksheet

The document is a worksheet on data file handling in Python, covering topics such as text and binary file operations, file modes, and various file handling functions. It includes questions and answers related to opening files, reading and writing data, and using functions like readline(), readlines(), flush(), and pickle for binary file handling. Additionally, it provides coding exercises to practice counting lines, occurrences of words, and manipulating file content.

Uploaded by

erendada07
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)
13 views5 pages

Data File Handling Class 12 Worksheet

The document is a worksheet on data file handling in Python, covering topics such as text and binary file operations, file modes, and various file handling functions. It includes questions and answers related to opening files, reading and writing data, and using functions like readline(), readlines(), flush(), and pickle for binary file handling. Additionally, it provides coding exercises to practice counting lines, occurrences of words, and manipulating file content.

Uploaded by

erendada07
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/ 5

WORKSHEET

DATA FILE HANDLING


1 Give one difference between Text file and Binary File
Ans Text file contains EOL character at the end of every line, there is no such character
in binary file
2 Write a Python statement to open a text file “DATA.TXT” so that new contents can
be written on it.
Ans f = open(„DATA.TXT‟,‟w‟)
3 Write a Python statement to open a text file “DATA.TXT” so that new content can be
added to the end of file
Ans f = open(„DATA.TXT‟,‟a‟)
4 Write a Python statement to open a text file “DATA.TXT” so that existing contents
can be read from file.
Ans f = open(„DATA.TXT‟)
5 A file “MYDATA.TXT” is opened as
file1 = open(“MYDATA.TXT”)
Write a Python statement to close this file.
Ans file1.close()
6 What is the different in file opening mode “a” and “w” ?
Ans “w” is used to write in file from the beginning. If file already exists then it will
overwrite the previous content.
“a” (append – add at the end ) is also used to write in file. If file already exists it will
write after the previous content i.e. it will not overwrite the previous content and
add new content after the existing content.
6 What is the significance of adding „+‟ with file opening mode, with context to „r+‟ ?
Ans “+” is used to add alternate action with specified mode i.e. if used with “r” as “r+” it
means it will allows to read and alternate action write.
7 What is the difference between readline() and readlines() ?
Ans readline() allows to read single line from file and return the content as string.
readlines() function will read all the lines from file and return it as a List of
lines/string.
8 What is the purpose of using flush() in file handling operations ?
Ans When we are writing data in file the content will be stored in file only when we close
the file. Before closing the file i.e. during the operations fill will be created but the
content will be in buffer not in file and when we close the file content will be shifted
to file from buffer.
flush() allows the user to send content in file before closing the file. It means when
flush() is used it will clear the buffer and transfer content to file.
9 What is the advantage of opening file using „with‟ keyword?
Ans With keyword reduces the overheads involve in file handling operations like closing
the file after operation or handling the file closing with exceptions. When file is
opened using “with” it will manage these things i.e. file will be automatically closed
after operations. It ensures the closing of file even if exceptions arises.
10 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

1|Page
Write the output of following statements –
f = open("CORONA.TXT")
sr1 = # to read first line of file
str2 = # to read next line of file
str3 = # to read remaining lines of file

Ans str1 = f.readline()


str2 = f.readline()
str3 = f.readlines() OR str3 = f.read()
11 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( )
12 What is the difference in write() and writelines()?
Ans write() function is used to write single string in file whereas writelines() function
allows to write List of strings
13 Considering the content stored in file “WORLDCUP.TXT”, write the output
India won the Cricket world cup of 1983
f = open(“WORLDCUP.TXT”)
print(f.read(2))
print(f.read(2))
print(f.read(4))
Ans In
di
a wo
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
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

3|Page
(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.

BINARY FILE HANDLING


1 Letter is prefixed to store string in binary form
Ans b
2 Write a Python statement to open a text file “DATA.TXT” in binary mode so that
new contents can be written on it.
Ans f = open('DATA.TXT','wb')
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
Ans f = open('DATA.TXT','ab')
4 Write a Python statement to open a text file “DATA.TXT” in binary mode so that
existing contents can be read from file.
Ans f = open('DATA.TXT','rb')
5 function is used in binary mode to send the read pointer to desired
position
Ans seek()

Note: seek() function of file object is used to reposition the cursor Syntax :
seek(number of bytes to read, seek_direction)
Seek_direction can be 0 – beginning, 1 – current position , 2- from last (can be in
negative also for backward traversing)

6 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()
Ans f.seek(-20,1)
7 Write a function SCOUNT() to read the content of binary file „NAMES.DAT‟ and
display number of records 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

8 To read and write collections like LIST, DICTIONARIES Python provides a module
called
Ans pickle
9 is the process of converting structures to byte stream before writing to
file.
4|Page
Ans Pickling
10 is the process of converting byte stream to original structure.
Ans Unpickling
11 Pickling is done by the function
Ans dump()
12 Unpickling is done by the function
Ans load()
13 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()
Ans pickle.dump(d,myfile)
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 pickle.load(myfile)
15 Python‟s standard streams are , ,
Ans stdin, stdout, stderr
16 Python‟s standard streams are available in module
Ans sys

5|Page

You might also like