WORKSHEET 4 For Text Files
WORKSHEET 4 For Text Files
THATCHUR.
Computer Science (083)
Class: XII Session: 2024-25
WORKSHEET - 4
CHAPTER : FILE HANDLING – TEXT FILE
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.
5 A file “MYDATA.TXT” is opened as
file1 = open(“MYDATA.TXT”)
Write a Python statement to close this file.
6 What is the different in file opening mode “a” and “w” ?
6 What is the significance of adding „+‟ with file opening mode, with context to „r+‟ ?
7 What is the difference between readline() and readlines() ?
8 What is the purpose of using flush() in file handling operations ?
9 What is the advantage of opening file using „with‟ keyword?
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
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
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
2|Page
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.
21. Write a function in python to read lines from file “POEM.txt” and display all those words,
which has two characters in it.
22. Write a method/function COUNTLINES_ET() in python to read lines from a text file
REPORT.TXT, and COUNT those lines which are starting either with ‘E’ and starting with ‘T’
respectively. And display the Total count separately.
23. Write a method/function SHOW_TODO() in python to read contents from a text file ABC.TXT
and display those lines which have occurrence of the word ‘‘TO’’ or ‘‘DO’’.
24. Write a Python program to find the number of lines in a text file ‘abc.txt’.
25. Write a method/function ISTOUPCOUNT() in python to read contents from a text file
WRITER.TXT, to count and display the occurrence of the word ‘‘IS’’ or ‘‘TO’’ or ‘‘UP’’
26. Write a function filter(oldfile, newfile) that copies all the lines of a text file “source.txt” onto
“target.txt” except those lines which starts with “@” sign.
27. Observe the following code and answer the question that follow:
File=open(“Mydata”,”a”)
____________# Blank 1
File.close()
What type (Text/Binary) of file is Mydata?
Fill the Blank 1 with statement to write ”ABC” in the file ”Mydata”.
28. A text file “PYTHON.TXT” contains alphanumeric text. Write a program that reads this
text file and writes to another file “PYTHON1.TXT” entire file except the numbers or
digits in the file.
29. Write a python code to find the size of the file in bytes, number of lines and number of words.
30. What is the difference between the following set of statements (a) and (b):
a) P = open(“practice.txt”,”r”)
P.read(10)
b) with open(“practice.txt”, “r”) as P:
x = P.read()
Bottom of Form
Q-2: Which of the following can be used to open a file called myText.txt in write-only mode?
Top of Form
A. outfile = open("myText.txt", w) B. outfile = open("myText.txt", “write”)
C. outfile = open("myText.txt", “w”) D. outfile = open("myText.txt")
Bottom of Form
Q-3: Which command below closes the already open file myText.txt if the following code has already been
written?
ref_file = open("myText.txt", "r")
Top of Form
A. close() B. ref_file.close() C. close(ref_file) D. close("myText")
Bottom of Form
Q-4: Which of the commands below is used to add the following string to the end of a file object filevar?
somestring = "my Sentence"
Top of Form
A. filevar.append(somestring) B. filevar.write("somestring")
C. filevar.write(somestring) D. somestring.write()
Check MeCompare me
Bottom of Form
4|Page