Python Program to Count the Number of Blank Spaces in a Text File

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Files in Python:

Python File handling is a method that allows you to save the program’s output to a file or read data from a file. In the programming world, file handling is a critical notion. File management is employed in practically every type of project. For example, suppose you’re developing an inventory management system. You have data connected to sales and purchases in the inventory management system, thus you must save that data somewhere. You can save that data to a file using Python file management. If you want to undertake data analysis, you must be given data in the form of a comma-separated file or a Microsoft Excel file. You can read data from a file and also store output back into it using file handling.

Given the file, the task is to count the number of blank spaces in a Text File.

Program to Count the Number of Blank Spaces in a Text File

Below is the full approach to calculate the total number of Blank Spaces in the given Text File.

Approach:

  • Take a variable say blankCount that stores the calculate the total number of blank spaces in a given file and initialize it to 0.
  • 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, Traverse 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.
  • Check to see if the character is a space using isspace, and if so, increase the blank count by 1.
  • Print the blankCount.
  • The Exit of the Program

Below is the implementation:

# Take a variable say blankCount that stores the calculate the
# total number of blank spaces in a given file and initialize it to 0.
blankCount = 0
# 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 words in wordslist:
            for char in words:
                # Check to see if the letter is a space using isspace,
                # and if so, increase the blank count by 1.
                if(char.isspace):
                    blankCount = blankCount+1
print('The total count of the blank spaces in the given file = ', blankCount)

Output:

Enter the file name = hello.txt
The total count of the blank spaces in the given file = 21

Explanation:

  • A file name 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 separated into a list of words ().
  • 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 iterated letter is a space, the letter count is increased.
  • The total number of blank space occurrences is printed.

Google Colab Images:

Files and Code:

Code:

Output Image:

Hello.txt

Related Programs: