Python program to Count Number of Lines, Words and Letters in a Text File

lines = 0
words = 0
letters = 0
fp = "C:/Users/USER/Desktop/Python.txt"
for line in open(fp):
 lines += 1
 letters += len(line)
 pos = 'out'
 for letter in line:
     if letter != ' ' and pos == 'out':
        words += 1
        pos = 'in'
     elif letter == ' ':
        pos = 'out'
print("Lines: ",lines)
print("Words: ",words)
print("Letters: ",letters)

 

 

Output:

 

Lines:  6
Words:  23
Letters:  88

   

 

Subscribe For Daily Updates