Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python count words in file

import re
# Open file in read mode
input_file = open("C:UsersfawazDesktopdata.txt", "rt")
"""
Get each line from file
Then decompose it into words
Update count with nb of words
per line.
"""
count = 0
for line in input_file: 
	words = re.split("s+", line.strip())  # words separated by any nb of white spaces
	print(words)
	count += len(words)

print("Number of words in input file:", count)

input_file.close()
 
PREVIOUS NEXT
Tagged: #python #count #words #file
ADD COMMENT
Topic
Name
4+6 =