Q:

Python program to count total number of uppercase and lowercase characters in file

belongs to collection: Python file handling programs

0

Programs can interact with files and data using the concept of file handling.

File Handling in Python is reading and writing data from files.

We will first read characters from the file and then character check whether it is uppercase (using isupper() method ) or lowercase (using islower() method ) and count its respective number and print the count.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to read data from files and count uppercase and lowercase character

# Program to count total number of 
# uppercase and lowercase characters in file

# Start of try block
try:
    #Counter for characters...
    upperCount = 0
    lowerCount = 0

    F=open("file.dat","r")
    while(True):
        data=F.read(1)
        if(data==""):
            break
        if (data.isupper()): 
                upperCount = upperCount + 1
        elif (data.islower()): 
                lowerCount = lowerCount + 1
                
    print(data,end='')
        
    print("Total Upper Case:",upperCount)
    print("Total lower Case:",lowerCount)
except FileNotFoundError as e:
    print(e)
finally:
    F.close()

File : File.dat

Learn Programming At IncludeHelp

Output:

Total Upper Case: 5
Total lower Case: 24

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Setting file offsets in Python... >>
<< Count the total number of uppercase characters in ...