Files in Python:
C program to count number of words in a text file: Files are identified locations on the disc where associated data is stored. They’re used to keep data in non-volatile memory for a long time (e.g. hard disk).
We use files for future usage of the data by permanently saving it because Random Access Memory (RAM) is volatile (it loses its contents when the machine is turned off).
We must first open a file before we can read from or write to it. When we’re finished, it needs to be closed so that the file’s resources may be released.
As a result, a file operation in Python is performed in the following order:
- Create a new file
- You can either read or write (perform the operations)
- Close the file.
Given a file, the task is to count words that end with a character ‘e’ in a given file in python
- Python Program to Split a String at First Instance of a Character
- Python Program to Reverse Each Word in a Text File
- Python Program to Reverse the Content of Text File and Store it in Another File
Program to Count Words in a Text File those are Ending with Alphabet “e” in Python
Below is the full approach for counting words that end with a character ‘e’ 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.
- Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
- Take a variable(which gives the count of the number of words ending with the alphabet “e”)and initialize its value with zero.
- Get the data from a file using the read() function and store it in a variable.
- Split the file data into a list of words using the split() function.
- Iterate in the above words list using the for loop.
- Check if the last character of each word is equal to ‘e’ using the negative indexing and if conditional statement.
- If it is true, Increment the count value by 1 and store it in the same variable.
- Print the count of a number of words ending with an alphabet “e”.
- Close the given file using the close() function.
- 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" # Open the file in read-only mode. In this case, we're simply just reading the contents of the file. gvn_file = open(givenFilename, 'r') # Take a variable(which gives the count of number of words ending with alphabet "e")and initialize its value with zero cnt = 0 # Get the data from a file using the read() function and store it in a variable file_data = gvn_file.read() # Split the file data into list of words using the split() function word_lst = file_data.split() # Iterate in the above words list using the for loop for wrd in word_lst: # Check if the last character of each word is equal to 'e' using the # negative indexing and if conditional statement. if wrd[-1] == 'e': # If it is true, Increment the count value by 1 and store it in the same variable. cnt+=1 # Print the count of number of words ending with an alphabet "e". print("The number of words in a file that ends with a character 'e'= ",cnt) # Close the given file using the close() function gvn_file.close()
Output:
The number of words in a file that ends with a character 'e'= 5
File Content:
welcome to btechgeeks give code in Python write a code in Python
Google Colab Images:
Files and Code: