Python Program To Count Number Of Words In A Text File

Python Program To Count Number Of Words In A Text File

Published on October 28 2021

In this tutorial, you will learn a python program to count the number of words in a text file.

Source code

file = open("myfile.txt","r")
count = 0
for line in file:
    words = line.split(" ")
    count += len(words)
file.close()
print("Number of words in a Text File : ", count)

myfile.txt

Python is an programming language.
Welcome to Programming Portal channel.
I love python.

 

Video