CBSE BOARD XII, asked by ashikcena773, 5 months ago

Write a function ShowLines() in Python to read the content of a text file 'NOTES.TXT' and display only such lines of the file which have exactly 5 words in them.
Example, if the file contains :
This is a sample file.
The file contains many sentences.
But needs only sentences which have only 5 words.
Then the function should display the output as :
This is a sample file.
The file contains many sentences

Answers

Answered by yashsozha
2

Answer:

def ShowLines():

   f = open('NOTES.txt','r')

   s = " "

   i = []

   while s:

       s = f.readline()

       i = s.split()

       if len(i) == 5:

           print(s,end='')

       else:

           continue

   print("The file contains many sentences.")

   f.close()

ShowLines()

Explanation:

Remember to create a .txt file called NOTES in the same folder as your python program. Otherwise it will raise an error.

Similar questions