count the number of words in a text file python code example
Example 1: Python program that takes a text file as input and returns the number of words of a given text file
with open('file1.txt','r+') as f:
lines=f.read()
words=lines.split()
print(len(words))
Example 2: Python program that takes a text file as input and returns the number of words of a given text file
with open('file1.txt','r+') as f:
numw=0
for line in f:
words= line.split()
print(words)
numw += len(words)
print(numw)