Adobe Scan 10-Sept-2024 4
Adobe Scan 10-Sept-2024 4
63
bank - Class Xl (Computer Science)
ByQuestion Chawla
Swati
F=open ("diary.txt", "r") data=F.readlines ()
strl=! Count=0
while strl: for lines in data:
strl=F. readline ()
print (str1, end=!')
line-lines. split)
for wOrd in line :
F.close ()
11. Write amethod in
if WOrd==" India!.
lines of text contentsPython to write
into a text file multiple
Count =
Ans,
line. myfile.txt
print (Count)
F.close( )
Count t1
F.close ()
print (W, end=!)
Or
W[0]==
line=lines.split)
for word in
line:
if len (word) <5:
14.
Write a method in Python to print ("Count Count=Count+1
of Small words
from a text file INDIA. read lines
TXT,to find and display
the occurrence of the word
",Count)
Ans, $India". 17. WriteF.close(
a
def CountIndia(): Python to metreadhod/funct ion BIGLINES) IO
file
lines from a textlines
F=open (" INDIA.
Question bank - Class XIl (Computer
By Swati Chawla
TXT", "r")
Science) 64
CONTENT.TXT, and
Ans. which are bigger than 20 display
those
characterS.
defBIGLINES):
(
T or t
F-open("ccontent.TXT", "r") Print ("The number of
data=F..readlines () :",T)
for line in data: f.close()
if len (line)>20: ETCount ()
toread lines from a
print (line, end=!') 0 Writea method in Python
and count those lines,
F.close () text file NOTES.TXT,
BIGLINES() which are ending with .' or ';
method/function ISTOUPCOUNTO Ans. Try to do yourself the
Vritea to count
21. Write a function in python STORY.TXT"
pytthonto read contents from a text file file
in
WRITER. TXT, to count and display the number of lines in a text
'A'.
oCCurrenceoffthe word IS» or TO" or UP". which is starting with an alphabet
ef
Ans. ISTOUPCOUNT(): Ans:
def COUNTLINES () :
'r')
c=0 file=open ('India. TXT',
file=open(India.TXT', 'r) lines =
file.readlines ()
line = file. read () count=0
wOrd = line.split () for w in lines :
for w in word: if w{O]=="A" or w[0]== "a":
if w=="IS" or w=="TO" or w=="UP": count=count+1
c=c+1 print("Total lines:",count)
print("Count of lS, TOand UP is ".c) file.close ()
file. close () COUNTILINES()
ISTOUPCOUNT ()
DISPLAYWORDSO
Write afunction ETCount) in Python, which 22. Write a method/function
lines from a text file
file
should read each character of a text in python to read which
uTESTFILE2.TXT and then count and STORY.TXT, and display those words,
alphabets are less than 4 characters.
display the count of occurrence of
Eand Tindividually (including smallcases e Ans. Try to do yourself () the following
and t too). CBSE Sample Paper (2022-23) 23. A text file Quotes.Txt" has
data written in it:
Example:
If the file content is as follows: Living a life youcan be proud of
Doing your best
activities
Today is a pleasant day. Spending your time with people and
that are important to you Standing up for
It might rain today. hard
things that are right even when it's
It is mentioned on weather sites Becoming the best version of you
Write a user defined function to display the
Ihe ETCount) function should display the total number of words present in the file.
output as:
The number of E or e: 6
Ans:
Ihe number of Tor t:9 def COUNTWORDS () :
c=0
file=open ('Quotes. txt', 'r')
tef
ETCount () :
Open ("testfile2. txt", "r")
line = file. read()
word = line. split()
data=f, read() for w in word:
E=T=0
tor i
C = c + 1
in data: print ("Total number of Words :
if i in 'Ee':
", c)
E+=1
if i in 'Tt': 24. Write a function Show words) in python to
T+=1 read the content of a text file 'NOTES.TXT"
print ("The number of E or e and display the entirecontent in capital letters.
65
sion nbank -Class XIl (Computer Science)
Swgi Chowlo
Example, if the file contains: "This is a test Lines = file. readlines()
file" for L in Lines:
Then the function should Compartment, 2020 W=L.split ()
as: THIS IS A TEST display the output if (len (W) ==5):
FILE print (L, end=)
Ans. #Method 1 file.close ()
def Show words (): Show words ()
file-open ('NOTES. TXT', 'r) 26. Write afunction countmy( )in Python to
Lines =
for L in file.readlines ()
the text file DATA.TXT"
and count rethade
number of times "my" occurs in the fle
Lines:
print (L.upper(),end=") For example if the file "DATA.TXT"
file.close ()
Show words ()
This is my website. I have
preferences in the CHOICE section,
The countmy( ) function should
displayedcontains,
#Method 2 output as: display the
def Show words (): "my occurs2 times".
Ans:
file=open ('NOTES. TXT', 'r')
L = def countmy () :
file.read ()
print (L.upper ())
f = open ('DATA. txt', 'r
Count = 0
file.close () x = f . read()
Show words () word = X . split ( )
25. Write a function Show for i in word :
read the content of a textwords) in python to
file 'NOTES.TXT" if ( i == 'my') :
and display only such lines of the file Count =Count + 1
have exactly 5 words in them. which
print ('my occurs' Count
Example, if the file contains: Itimes')
"This is a sample file. 27. Atext file "Quotes.Txt" has the
The file contains many data written in it: following
But need only sentencessentences.
which have only 5 Living a life you can be proud of
Words. '" Doing your best
Then the function should display the Spending your time with people and activities
output
as:
Compartment, 2020 that are important to you
This is a sample file. Standing up for things that are right even when
The file contains many it's hard
sentences.
Ans. #Display those lines having 5 Becoming the best version of you
WOrds. Write a user defined function
#Method 1 total number of words present intothedisplay the
def Show words () : Ans. file.
def
line= ! Words () :
f=open ("Notes.txt", "r") f=open ("Quotes.txt", "r")
while True: L=f.read ()
if line==!: Count=0
else:
break words=L.split
for i in
()
words:
line=f.readline () Countt=1
Words=line.split () print ("Total nunber of wordD
if len (words) ==5: :"Count)
28. Write a
print (1ine, end=") method in Python
text file Poem.txt" to read
and display
lines from
those words ,
Show words ()
#Method 2 which are ending with 'e.
Ans:
def Show words():
file=open ('NOTES. TXT','r') def read() :
f=open ("Poem. txt", 'r')
Question bank - Class XIl (Computer Science) 66
By Swati Chawla
k=f.read()
did not
s=k.split() there was too much crowd, which she
s:
for 1 ln like.
if i[-1] =='e': visit some hill
So thistime she decided toCompartment, 2021
print (i, end=! ) station.
f.close()
Writea method in Python to read lines from Ans.
atextfile Story.txt" and calculate and display #Method 1
thesumof allIthe even digits present in a text def ChangeGender () :
I=open ("BIOPIC. TXT", 'r')
file
Text=f.read ()
'," she
sefread(): Print (Text.replace (! he
f-open ("PoemDigit.1txt", 'r') ))
s=0 f.close ()
k=f. read ()
for i in k: #Method 2
if i.isdigit ():
def ChangeGender () :
if int(i)%2==0: 'r')
With open ('BIOPIC. TXT',
S=s+int (i) as File:
print (s) Lines = File.readlines
()
f.close () for L in Lines:
M a function Remove_Lowewrcase) that
Write Words = L.split ()
srcepts two file names, and copies all the lines for W in Words:
thaf do not start withlowercase letter from the if W == 'he':
first file into second file. W = 'she!
Ans.
(): print (W, end = ! )
isf Remove LowerCase print ()
f=open ("Poem. txt", "r")
p=open ("Poeml.txt", "w") ChangeGender )
x=f.readlines () 32. Write the definition of a function
for i in x: CountLine() in Python, which should read
if i[0].islower (): each line of a text file "SHIVAJI.TXT" and
continue count total number of lines present in text
else: file.
p.write (i) For exanmple, if the content of the file
"SHIVAJI. TXT" is as follows :
f.close ()
p.close() Shivaji was born in the family of Bhonsle.
He was devoted to his mother Jijabai.
Write the definition of afunction India at that time was under Muslim rule.
ChangeGender() in Python , which reads the
Contents ofa text file "BIOPIC.TXT" and The function should read the file content and
display the output as follows :
isplays the content of the file with every Total number of lines : 3
currence of the word he' replaced by 'she'. Compartment, 2021
For example, if the content of the file Ans.
"BIOPIC. TXT" is as follows: def Count Line () :
Last time he went to f=open ("SHIVAJI. TXT", 'r')
Agra,
there was too much crowd, which he did not Lines=f.readlines ()
ike, print('Total number of lines :',
So this time he decided to visit some hill len (Lines) )
f.close ()
Station. Count Line ()
The function should read the file content and
33. Write a method/function
isplay the output follows:
Last DISPLAYWORDSO in python to read lines
time she went to
Agra,
n 67
bank - Class SXl (Computer Science)
Nti Chowa
from a text file
STORY.TXT,
those words, which and display
are less than 4
(
characters.
CBSE Sample Paper (2019-20)
Ans.
def DISPLAYWORDS ():
file=open(STORY.TXT'
line = file.read() ,' )
word = line. split()
for w in wOrd:
if len (w) <4:
print ( w)
file.close ()
34. Write a function in python to
count the
number of lines in a text file STORY.TXT
which is starting with an alphabet A'
Ans.
CBSE Sample Paper (2019-20)
def COUNTLINES ():
file=open('STORY. TXT', r')
lines = file.readlines ()
Count=0
for w in lines:
if w[0]=="A" or w[0]=="a":
Count=count+1
print ("Total lines",count)
file.close ()
Draoico Ouetions of Rinary Files