English

Write the definition of a Python function named LongLines() which reads the contents of a text file named 'LINES.TXT' and displays those lines from the file which have at least 10 words in it.

Advertisements
Advertisements

Question

Write the definition of a Python function named LongLines() which reads the contents of a text file named 'LINES.TXT' and displays those lines from the file which have at least 10 words in it. For example, if the content of 'LINES.TXT' is as follows: 

Once upon a time, there was a woodcutter
He lived in a little house in a beautiful, green wood.
One day, he was merrily chopping some wood.
He saw a little girl skipping through the woods, whistling happily.
The girl was followed by a big gray wolf. 

Then the function should display output as:

He lived in a little house in a beautiful, green wood.
He saw a little girl skipping through the woods, whistling happily.

Code Writing
Advertisements

Solution

def LongLines():
       f=open(“Lines.txt","r")
       lst=f.readlines()
       for ln in lst:
           Line=ln.split(' ')
           if len(Line)>=10:
              print(ln)
       f.close()
shaalaa.com
Reading from a Text File in Python
  Is there an error in this question or solution?
2022-2023 (March) Set 4
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×