Count the total number of characters in a file

In this program, we will count the total number of characters in a file

Steps to solve the program
  1. Create a variable to count the total number of characters and assign its value equal to 0.
  2. Open the desired file using open() in reading mode.
  3. Use for loop iterate over the data.
  4. If the element is character then add 1 to the variable that we have created.
  5. Use isalpha() for this purpose.
  6. Print the output.
				
					char=0

f=open("file2.txt","r")
f1=f.read()
for ele in f1:
    if ele.isalpha():
        char+=1
f.close()

print("Number of characters: ",char)
				
			

Output :

				
					26
				
			

print 1-10 natural numbers but it should stop when the number is 6

find the total number of digits in a file

Leave a Comment