In this program I will show you how to count the number of words from a text file using Python programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

Program Listing

data.txt

Python Programming is Fun and Profit
Professor Jake Rodriguez Pomperada


reading_textfile.py

# reading_textfile.py
# Mr. Jake Rodriguez Pomperada, MAED-IT, MIT
# www.jakerpomperada.com www.jakerpomperada.blogspot.com
# jakerpomperada@gmail.com
# April 3, 2021
# Bacolod City, Negros Occidental

print()
print("\tCount Number of Words From a Text File in Python");
print()
try:
    print()
    fr = open("data.txt","r")
    for line in fr:
        line = line.strip("\n")
        print(line.upper())
        words = line.split()

        print()
        print('Total number of words: ', len(words))
        print("-----------------------------------------------")
        print()
        print("\tEND OF PROGRAM");
        print()
    fr.close()
except FileNotFoundError:
    print("File is not found")

Leave a Reply

Your email address will not be published. Required fields are marked *