File Handling 11: File line, word and character count

# Count number of characters, words and lines in a file
# File Count

fs = open("ipt.txt", "r")

charcount = 0
wordcount = 0
linecount = 0

all_lines = fs.readlines()
linecount = len(all_lines)

for Line in all_lines:
Line = Line.strip().lower()

tmp = Line.split(" ")
wordcount += len(tmp)

for ch in Line:
if(ch.isalpha()):
charcount += 1

print("Number of characters in file =", charcount)
print("Number of words in file =", wordcount)
print("Number of lines in file =", linecount)

fs.close()

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu