import sys # exit() # returns the number of capital letters in chars def countCapitals( chars ): capitals = 0 # number of capitals found # For each character in chars, # increase the counter if its a capital letter for ch in chars: if ch.isupper(): capitals += 1 return capitals filename = 'info.txt' mode = 'w' # write mode numCapitals = 0 # total number of capitals found # Step 1) Open the file # Step 2) Verify successful file open try: infoInputFileObj = open( filename, 'r') # Step 3) Read from or write to the file # loop through each line in the file and # count the number of capitals in that line line = infoInputFileObj.readline() # priming read # read until the end of the file while line != '': numCapitals += countCapitals( line ) line = infoInputFileObj.readline() except: print('ERROR! Unable to open or read from', filename) sys.exit() # Step 4) Close the file infoInputFileObj.close() print( f'Found {numCapitals} capital letters in {filename}')