Python : Python script to find all files with a specific extension in a given folder, and do a find and replace

python @

In this article, we will guide you through writing a Python script to find all files with a .sh extension in a given folder, and replace a specific term (“fres hers view”) with another (“”). The steps we will be taking are as follows:

  1. Identify all .sh files in a given directory.
  2. Open each .sh file and read the contents.
  3. Find the term to be replaced in the file content.
  4. Replace the term with the new term.
  5. Write the updated content back to the file.

Python Script

Here is the Python code for accomplishing the above steps.

import os

def replace_term_in_files(directory, old_term, new_term):
    # walk through directory
    for foldername, subfolders, filenames in os.walk(directory):
        for filename in filenames:
            # only consider .sh files
            if filename.endswith('.sh'):
                file_path = os.path.join(foldername, filename)

                with open(file_path, 'r') as file:
                    file_content =

                # replace old_term with new_term
                updated_content = file_content.replace(old_term, new_term)

                # write updated content back to the file
                with open(file_path, 'w') as file:

replace_term_in_files('/freshers/jobs/shell/scripts/', 'fres hers view', '')


  1. Importing necessary modules: We need the os moduleto work with the file system.
  2. Defining the function: The function replace_term_in_files() takes three arguments: directory, old_term, and new_term. The directory is where the script will start searching for .sh files, old_term is the string we’re looking to replace, and new_term is the string we’ll replace old_term with.
  3. Walking the directory:os.walk(directory) returns a generator, that produces a tuple of values – name of the folder, names of the subfolders in the folder, and names of all files in the folder, for each of the directories it traverses. The script uses this to go through each file in the directory, as well as in its subdirectories.
  4. Checking file extension: For each file, it checks if the file ends with ‘.sh’. If it does, it continues to the next steps, else it moves on to the next file.
  5. Opening and reading file: The script opens the file in read mode and reads the content.
  6. Replacing term: The replace() function is a string function in Python that is used to replace occurrences of a substring within a string. We use this to replace all occurrences of old_term with new_term in file_content.
  7. Writing to the file: The script then opens the file in write mode and writes the updated content back to the file. This overwrites the original file content.

Note: This script does not keep a backup of the original files. If you make a mistake in the old_term or new_term, you cannot recover the original files. So, please use this script with caution.

Refer more on python here :
Author: user

Leave a Reply