CS12_DFH_TEXT
CS12_DFH_TEXT
CS12_DFH_TEXT
3. To read the 4th line from the text file, which of the following statements is true? 1
a. dt = f.readlines();print(dt[3]) b. dt=f.read(4) ;print(dt[3])
c. dt=f.readline(4);print(dt[3]) d. All of these
6. Write a function countVowels() in Python, which should read each character of a text file myfile.txt”, count the number
of vowels and display the count. 3
Example: If the “myfile.txt” contents are as follows:
This is my first class on Computer Science.
The output of the function should be: Count of vowels in file: 10
8. Write a function DISPLAYWORDS( ) in python to display the count of words starting with “t” or “T”in a text file
‘STORY.TXT’. 3
9. ka win of class 12 is asked by his Computer Science teacher to count the number of “the” and “and” in a text file
“Book.txt”. He was able to do the task to some extent, but was confused in some areas. Help him out to complete the task.
def displaytheand():
num=0
_________ #Statement 1
N=f.read()
M=N.split()
for x in M:
if x=="the" or x== "and":
print(x)
num=num+1
__________ #Statement 2
print("Count of the/My in and:",num)
1. Which of the following is the correct one to be used in the place of statement 1 1
a) f=open("Book.txt","w") b) f=open("Book.txt","r")
c) f=open("Book.txt","a") d) F=open("Book.txt","r")
2. Identify the correct statement that may be used in the place of statement 2. 1
a) f.close() b) f.close(“book.txt”)
c) close() d) None of the above
9. Kum.Anisha wants to develop a program to count the number of “Me” or “My” words present in a text file
“STORY.TXT” . But she is not sure about the code . Help her to complete it. 5
def displayMeMY():
n=0
f=open(“story.txt” , „____‟) #line 1
N = __read() #line2
M = N.________ #line 3
for x in M :
if ____________________: #line 4
n=n+1
f.________ #line 5 Closing the file.
print(“Count of Me /My words : “, ____) #line 6