Python Program to find N Character Word in the Given Text File

Files In Python:

A file is a piece of data or information stored on a computer’s hard drive. You’re already familiar with a variety of file kinds, including music, video, and text files. Manipulation of these files is trivial with Python. Text files and binary files are the two types of files that are commonly used. Binary files contain binary data that can only be read by a computer, whereas text files include plain text.

For programmers and automation testers, Python file handling (also known as File I/O) is a crucial topic. Working with files is required in order to write to or read data from them.

In addition, if you didn’t know, I/O activities are the most expensive techniques via which software might fail. As a result, when implementing file processing for reporting or any other reason, you should proceed with caution. The construction of a high-performance application or a robust solution for automated software testing can benefit from optimizing a single file activity.

Given a File, the task is to find all the n length words in the given text file and print them.

Program to find N Character Word in the Given Text File in Python

Method #1: Using For loop (Static Input)

Approach:

  • Give the value of n as static input and store it in a variable.
  • 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.
  • Iterate through the lines of the file using the For loop.
  • Split the words of the line using the split() function and store them in a variable(it is of type list).
  • Loop in the above list using another Nested For loop.
  • Check the length of the word is equal to the given value n using the If conditional statement.
  • If it is true then print it using the print() function.
  • The Exit of the Program.

Below is the implementation:

# Give the value of n as static input and store it in a variable.
n = 6
# 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 reading the contents of the file.
with open(givenFilename, 'r') as givenfilecontent:
    # Iterate through the lines of the file using the For loop.
    print('The words of length', n, 'are :')
    for gvnfileline in givenfilecontent:
      # Split the words of the line using the split() function and store them in a variable(it is of type list).
        gvnfilewords = gvnfileline.split()
        # Loop in the above list using another Nested For loop
        for word in gvnfilewords:
            # Check the length of the word is equal to the given value n using the If conditional statement.
            if(len(word) == n):
                # If it is true then print it using the print() function.
                print(word)

Output:

The words of length 6 are :
python
coders

Explanation:

  • The file path is stored in the variable ‘file name.’ Change the value of this variable to the path of your own file.
  • Dragging and dropping a file onto the terminal will show its path. The code will not run unless you change the value of this variable.
  • The file will be opened in reading mode. Use the open() function to open a file. The path to the file is the method’s first parameter, and the mode to open the file is the method’s second parameter.
  • When we open the file, we use the character ‘r’ to signify read-mode.
  • Get all the words of the line using the split() function and check if the length of the word is equal to n using the if conditional statement.
  • If it is true then print the word.

Method #2: Using For loop (User Input)

Approach:

  • Give the value of n as user input using the input() function and store it in a variable.
  • 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.
  • Iterate through the lines of the file using the For loop.
  • Split the words of the line using the split() function and store them in a variable(it is of type list).
  • Loop in the above list using another Nested For loop.
  • Check the length of the word is equal to the given value n using the If conditional statement.
  • If it is true then print it using the print() function.
  • The Exit of the Program.

Below is the implementation:

# Give the value of n as static input and store it in a variable.
n = int(input('Enter the value of length of word to print : '))
# 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 reading the contents of the file.
with open(givenFilename, 'r') as givenfilecontent:
    # Iterate through the lines of the file using the For loop.
    print('The words of length', n, 'are :')
    for gvnfileline in givenfilecontent:
      # Split the words of the line using the split() function and store them in a variable(it is of type list).
        gvnfilewords = gvnfileline.split()
        # Loop in the above list using another Nested For loop
        for word in gvnfilewords:
            # Check the length of the word is equal to the given value n using the If conditional statement.
            if(len(word) == n):
                # If it is true then print it using the print() function.
                print(word)

Output:

Enter the value of length of word to print : 5
The words of length 5 are :
hello
btech

Samplefile.txt:

hello this is btechgeeks python programming language
for btech students and coders

Sample Implementation in google colab:

Leave a Comment