Write Python Program to Count the Number of Words in a Text File


Topic: Write Python Program to Count the Number of Words in a Text File

Solution

fname = input("Enter file name: ")
num_words = 0
with open(fname, 'r') as f:
    for line in f:
        words = line.split()
        num_words += len(words)
print("Number of words:")
print(num_words)



List all Python Programs