Python Program that Reads a Text File and Counts the Number of Times a Certain Letter Appears in the Text File

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Files in Python:

A file is a piece of information or data that is kept in computer storage devices. You are already familiar with several types of files, such as music files, video files, and text files. Python makes it simple to manipulate these files. In general, files are classified into two types: text files and binary files. Text files are plain text, whereas binary files include binary data that can only be read by a computer.

Python file handling (also known as File I/O) is an important topic for programmers and automation testers. It is necessary to work with files to either write to or read data from them.

Furthermore, if you are not already aware, I/O operations are the most expensive procedures through which a program might fail. As a result, you should take caution while implementing file handling for reporting or any other reason. Optimizing a single file activity can contribute to the development of a high-performance application or a solid solution for automated software testing.

Given a file and a character, the task is to count the frequency of the given character in the given file.

Program that Reads a Text File and Counts the Number of Times a Certain Letter Appears in the Text File in Python

Below is the full approach to read a text file and count the number of times the given character appears in the given text file.

Approach:

  • Take a variable say charcount, that stores the count of the given character and initialize it to 0.
  • Scan the given character using the input() function and store it in a variable.
  • Create the file or upload the existing file.
  • Enter the file name of the file using the input() function and store it in a variable.
  • In read mode, open the file with the entered file name.
  • Using for loop, go over the lines in the file.
  • Split the line into words using the split() function.
  • To traverse the words in the list, use a for loop, then another for loop to traverse the letters in the word.
  • If the character given by the user and the character encountered during an iteration are equal, increment the charcount by 1.
  • Print the charcount.
  • The exit of the program.

Below is the implementation:

# Take a variable say charcount, that stores the count of the given character and initialize it to 0.
charcount = 0
# Scan the given character using the input() function and store it in a variable. 
givenchar = input('Enter some random character = ')
# Enter the file name of the  file using the input() function and store it in a variable.
filename = input("Enter the file name = ")
# In read mode, open the file with the entered file name.
with open(filename, 'r') as givenfile:
  # Using for loop, go over the lines in the first file.
    for fileline in givenfile:
        # Split the line into words using the split() function.
        wordslist = fileline.split()
        # To traverse the words in the list, use a for loop, then another
        # for loop to traverse the letters in the word.
        for word in wordslist:
            for character in word:
                # If the character given by the user and the character encountered
                # during an iteration are equal, increment the charcount by 1.
                if(character == givenchar):
                    charcount = charcount+1
print('The count of the character {', givenchar, '} =', charcount)

Input File:

Guido van Rossum created Python, an interpreted, object-oriented, high-level programming language with dynamic semantics.
It was first published in 1991. The name "Python" is a tribute to the British comedy troupe Monty Python and is meant to
be simple as well as entertaining. Python has a reputation for being a beginner-friendly language, and it has surpassed 
Java as the most generally used beginning language because it takes care of much of the complexity for the user, allowing
beginners to concentrate on mastering programming ideas rather than minute details.
Python is popular for Rapid Application Development and as a scripting or glue language to tie existing components 
together because of its high-level, built-in data structures, dynamic typing, and dynamic binding. It is also used for
server-side web development, software development, mathematics, and system scripting. Python's programme maintenance
costs are lower because of its simple syntax and emphasis on readability. Modular applications and code reuse are further
made easier with Python's support for modules and packages.
Python is an open source community language, which means that libraries and features are constantly being developed by 
individual programmers.

Output:

Enter some random character = s
Enter the first file name = hello.txt
The count of the character { s } = 70

Explanation:

  • A file name and the letter to be searched must be entered by the user.
  • In read mode, the file is opened with the open() method.
  • To read through each line of the file, a for loop is utilized.
  • Using split, each line is divided into a list of terms ().
  • A for loop is used to go through the list of words, and another for loop is used to go through the letters of the word.
  • If the letter provided and the letter found during the iteration is the same, the letter count is increased.
  • The total number of times the letter appears is printed.

Google Colab Images:

Files and Code:

Code:

Output Image:

Hello.txt:

We calculated the count of the character ‘s’ from this file.
Related Programs: