Python to String Search From Multiple Files with Full Source Code For Beginners

String search from multiple files:

  • Finds a file with the inputted string in the specified folder of your choice.

Prerequisites:

  • Python3 is the only prerequisite! No external modules are needed to run.

Run the Script:

  • In order to run this script, you must have Python3 installed, not Python2.
  • The command to run below, and you’ll be prompted with two questions, the string to search, and where to look.

Run the Script:

python3 findstring.pyCode language: CSS (css)

Source Code:

findstring.py

import os

text = input("input text : ")

path = input("path : ")

# os.chdir(path)


def getfiles(path):
    f = 0
    os.chdir(path)
    files = os.listdir()
    # print(files)
    for file_name in files:
        abs_path = os.path.abspath(file_name)
        if os.path.isdir(abs_path):
            getfiles(abs_path)
        if os.path.isfile(abs_path):
            f = open(file_name, "r")
            if text in f.read():
                f = 1
                print(text + " found in ")
                final_path = os.path.abspath(file_name)
                print(final_path)
                return True

    if f == 1:
        print(text + " not found! ")
        return False


getfiles(path)Code language: PHP (php)

Leave a Comment