Files in Python:
Python file handling is a way of saving program output to a file or reading data from a file. File handling is a crucial concept in the programming world. File management is used in almost every form of project. Assume you are constructing an inventory management system. You have data in the inventory management system related to sales and purchases, thus you must save that data somewhere. Using Python’s file management, you can save that data to a file. You must be given data in the form of a comma-separated file or a Microsoft Excel file if you wish to conduct data analysis. Using file handling, you can read data from a file and write output back into it.
Given a file, the task is to count the number of uppercase, lowercase, spaces in a given file in Python.
- Python Program to Capitalize the First Letter of Every Word in the File
- Python Program to Count Words in a Text File those are Ending with Alphabet “e”
- Python Program to Count Number of Digits in a Text File
Program to Count Number of Uppercase, Lowercase, Space in a Text File in Python
Below is the full approach for counting the number of uppercase, lowercase, spaces in a given file in Python.
Approach:
- Initialize all the uppercase, lowercase, digits, spaces, special characters count to zero.
- Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
- Pass the given filename, r (represents read-only mode) as arguments to the open() function to open the given file.
- Iterate in the above text of the file using the for loop.
- Iterate in the characters of the line using nested for loop.
- Check if the ASCII value of character is greater than or equal to ‘A’ and less than or equal to ‘Z’ using the if conditional statement.
- If it is true then increment the uppercase count value by 1.
- Check if the ASCII value of character is greater than or equal to ‘a’ and less than or equal to ‘z’ using the elif conditional statement.
- If it is true then increment the lowercase count value by 1.
- Check if the character is greater than 0 and less than or equal to ‘9’ using the elif conditional statement.
- If it is true then increment the digit count value by 1.
- Check if the character is equal to space(‘ ‘) using the elif conditional statement.
- If it is true then increment the spaces count value by 1.
- Else increment the special characters count value by 1.
- Print the number of uppercase characters in a given file.
- Print the number of lowercase characters in a given file.
- Print the number of digits in a given file.
- Print the number of spaces in a given file.
- Print the number of special characters in a given file.
- The Exit of Program
Below is the implementation:
# Initialize all the uppercase, lowercase, digits, spaces, special characters count to zero. uppr_cnt = lwr_cnt = dig_cnt = space_cnt = spclchrs_cnt = 0 # Make a single variable to store the path of the file. This is a constant value. # This value must be replaced with the file path from your own system in the example below. givenFilename = "samplefile.txt" # Pass the given filename, r (represents read-only mode) as arguments to the open() function # to open the given file file = open(givenFilename, "r") # Iterate in the above text of the file using the for loop for line in file: # Iterate in the characters of the line using nested for loop for charactrs in line: # Check if the ASCII value of character is greater than or equal to 'A' and less than or equal to 'Z' # using the if conditional statement. if charactrs >= 'A' and charactrs <= 'Z': # If it is true then increment the uppercase count value by 1 uppr_cnt = uppr_cnt+1 # Check if the ASCII value of character is greater than or equal to 'a' and less than or equal to 'z' # using the elif conditional statement. elif charactrs >= 'a' and charactrs <= 'z': # If it is true then increment the lowercase count value by 1 lwr_cnt = lwr_cnt+1 # Check if the character is greater than 0 and less than or equal to '9' # using the elif conditional statement. elif charactrs > '0' and charactrs <= '9': # If it is true then increment the digit count value by 1 dig_cnt = dig_cnt+1 # Check if the character is equal to space(' ') # using the elif conditional statement. elif charactrs == ' ': # If it is true then increment the spaces count value by 1 space_cnt = space_cnt+1 else: # Else increment the special characters count value by 1 spclchrs_cnt = spclchrs_cnt+1 # Print the number of uppercase characters in a given file print("The number of uppercase characters =", uppr_cnt) # Print the number of lowercase characters in a given file print("The number of lowercase characters =", lwr_cnt) # Print the number of digits in a given file print("The number of digits in a given file =", dig_cnt) # Print the number of spaces in a given file print("The number of spaces in a given file =", space_cnt) # Print the number of special characters in a given file print("The number of special characters in a given file=", spclchrs_cnt)
Output:
The number of uppercase characters = 3 The number of lowercase characters = 24 The number of digits in a given file = 6 The number of spaces in a given file = 6 The number of special characters in a given file= 3
File Content:
GoodMorning 123 this is 567 Btechgeeks @#%
Google Colab Images:
Files and Code:
Output: