6, అక్టోబర్ 2025, సోమవారం

Get number of characters, words, spaces and lines in a file - Python............ concept 48/50 ...........Number of words in text file: 25 Number of lines in text file: 4 Number of characters in text file: 91 Number of spaces in text file: 21

 def counter(fname):

    # variable to store total word count.........  num_words = 0

   # variable to store total line count..........  num_lines = 0

   # variable to store total character count..........  num_charc = 0

   # variable to store total space count..................num_spaces = 0

    # opening file using with() method  so that file gets closed   after completion of work

    with open(fname, 'r') as f:

    # loop to iterate file line by line

        for line in f:

   # incrementing value of num_lines with each iteration of loop to store total line count

            num_lines += 1

     # declaring a variable word and assigning its value as because every file is supposed to start with a word or a character

            word = 'Y'

  # loop to iterate every line letter by letter

            for letter in line:

  # condition to check that the encountered character is not white space and a word

                if (letter != ' ' and word == 'Y'):

 # incrementing the word count by 1

                    num_words += 1

 # assigning value N to variable word because until  space will not encounter a word can not be completed

                    word = 'N'

 # condition to check that the encountered character is a white space

                elif (letter == ' '):

 # incrementing the space count by 1

                    num_spaces += 1

  # assigning value Y to variable word because afterwhite space a word is supposed to occur

                    word = 'Y'

  # loop to iterate every character

                for i in letter:

  # condition to check white space

                    if(i !=" " and i !="\n"):

  # incrementing character count by 1

                        num_charc += 1

  # printing total word count  ...... print("Number of words in text file: ",  num_words)

 # printing total line count.........print("Number of lines in text file: ",   num_lines)

 # printing total character count....print('Number of characters in text file: ',num_charc)

  # printing total space count............ print('Number of spaces in text file: ',num_spaces)

    # Driver Code:

if __name__ == '__main__':

    fname = 'File1.txt'

    try:

        counter(fname)

    except:

        print('File not found')

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి