Write a function WordCount() in Python to read a text file “Mydiary.txt” and
display no of words present in each line.
Example:
If the file content is as follows:
Updated information
As simplified by official websites.
The function should display the output as:
Line No 1 : Words=2
Line No 2 : Words=5
Answers
Answered by
4
def WordCount(file):
with open(file) as f:
for idx, line in enumerate(f):
print(f"Line No {idx + 1}: Words = {len(line.split())}")
WordCount("Mydiary.txt")
Similar questions