file.py
# Write a function in Python to count and display the total number of words in a text file.
def countWords(file_name):
file=open(file_name)
lines=file.readlines()
count=0
for line in lines:
words=line.split()
count+=len(words)
return count
#calling function
print("Number of words in file = ",countWords("file.txt"))
output:
Output
Number of words in file = 108