Skip to content
Question

Q.(a) 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.

(OR)
(b) Write a function count_Dwords() in Python to count the words ending with a digit in a text file "Details.txt". Example: If the file content is as follows: On seat2 VIP1 will sit and On seat1 VVIP2 will be sitting Output will be: Number of words ending with a digit are 4
CBSECBSE Class XII Board 2023Subjective· 3mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Part (a): print lines of LINES.TXT with 10 or more words. Part (b): count words in Details.txt that end with a digit (answer 4 for the sample).

Both tasks follow the same file pattern: open, iterate, split into words, test a condition.

Part (a)

Read the file line by line. line.split() returns the list of words on that line, and we print the line when it has 10 or more of them. end="" avoids adding a second newline, since the line already carries its own.

def LongLines():
    f = open("LINES.TXT", "r")
    for line in f:
        words = line.split()
        if len(words) >= 10:
            print(line, end="")
    f.close()

Counting words in the sample, only these two lines reach 10+ words:

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

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.