Files in Python:
One of the most important subjects for programmers and automation testers is Python file handling (also known as File I/O). It is necessary to work with files in order to write to or read data from them.
Also, if you didn’t know, I/O operations are the most expensive processes where a programme can go wrong. As a result, you must use extreme caution while implementing file processing for reporting or any other reason. Optimizing a single file action can help in the creation of a high-performing application or a reliable automated software testing solution.
Consider the following scenario: you’re planning to construct a large Python project with a large number of workflows. Then it’s unavoidable that you don’t make a log file. You’ll also be handling the log file’s read and write activities. Debugging huge applications with log files is a terrific way to go. It’s usually better to consider a scalable design from the start, as you won’t be sorry later if you didn’t.
Given a file, the task is to count the number of digits in a given file in Python.
- Python File close() Method with Examples
- Python File write() Method with Examples
- Python File isatty() Method with Examples
Program to Count Number of Digits in a Text File in Python
Below is the full approach for counting the number of digits in a given file in Python.
Approach:
- 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) as arguments to the open() function to open the given file.
- Read the above file using the read() function(get the content) and store it in a variable.
- Take a variable(which gives the count of digits in a file) and initialize its value with zero.
- Check if each character of the file text is digit or not using the isdigit() function and if conditional statement.
- If it is true, then increment the value of the above-initialized variable(digit_cnt) by 1.
- Print the count of the number of digits in a given file.
- Close the above file.
- The Exit of Program.
Below is the implementation:
# 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) as arguments to the open() function # to open the given file file = open(givenFilename,"r") # Read the above file using the read() function(get the content) and store it in a variable filetext = file.read() # Take a variable(which gives the count of digits in a file) and initialize its value with zero. digit_cnt = 0 # Iterate in the above text of the file using the for loop for chr in filetext: # Check if each character of the file text is digit or not using the isdigit() function # and if conditional statement if chr.isdigit(): # If it is true, then increment the value of the above initialized # variable(digit_cnt) by 1 digit_cnt += 1 # Print the count of number of digits in a given file print("The count of number of digits in a given file = ",digit_cnt) # Close the above file file.close()
Output:
The count of number of digits in a given file = 6
File Content:
hello 123 this is 567 btechgeeks
Google Colab Images:
Files and Code:
samplefile.txt: