Python Program to Compute the Number of Characters, Words and Lines in a File

Aim:

Write a python program to compute the number of characters, words and lines in a file.

Program:

k=open('D:/a.txt','r')
char,wc,lc=0,0,0
for line in k:
for k in range(0,len(line)):
char +=1
if(line[k]==' '):
wc+=1
if(line[k]=='\n'):
wc,lc=wc+1,lc+1 
print("The no.of chars is %d\n The no.of words is %d\n The
no.of lines is %d"%(char,wc,lc)) 

Execution:

Input:
a.txt contain
khit
Guntur
Hyderabad 

Output:
The no. of chars is 21
The no. of words is 2
The no. of lines is 2 

Leave a Comment