String search from multiple files use python

codepythonic
0

  

 String search from multiple files use python 

 Finds a file with the supplied string in the folder of your choosing.

Install  Requirement : 

$ pip install os 


import os
text = input("input text: ")
path = input("path: ")

def search_files(path):
    found = False
    os.chdir(path)
    files = os.listdir()

    for file_name in files:
        abs_path = os.path.abspath(file_name)

        if os.path.isdir(abs_path):
            found = search_files(abs_path)
            if found:
                break

        if os.path.isfile(abs_path):
            with open(abs_path, "r") as f:
                if text in f.read():
                    found = True
                    print(f"{text} found in:")
                    print(abs_path)
                    break

    if not found:
        print(f"{text} not found!")

    return found

search_files(path)

In this updated version, the search_files function is defined to search for the given string recursively within the specified folder. It correctly handles subdirectories and checks if the string is present in each file.

Make sure to provide the desired string and path when prompted. The function will then search for the supplied text within the files in the specified folder and print the path of any file where the string is found. If the string is not found in any file, it will display a corresponding message.


How to use this code :


Let's go step-by-step on how to use the provided Python code to search for a specific string in files within a folder:

  1. Copy the code: Copy the entire Python code provided in the previous response and paste it into a Python script file (e.g., string_search.py) using a text editor or an Integrated Development Environment (IDE).

  2. Run the script: Open a terminal or command prompt, navigate to the directory where you saved the string_search.py file, and run the script using the Python interpreter:

python string_search.py
  1. Input the search text: After running the script, it will prompt you to enter the text you want to search for in the files.
input text: <type your search text here>

Type the text you want to search for and press Enter.

  1. Input the folder path: Next, the script will prompt you to enter the path of the folder where you want to search for the text.
path: <type the folder path here>

Type or paste the path of the folder (directory) where you want to search for the specified text and press Enter.

  1. View the search results: The script will recursively search for the given text in all the files within the provided folder and its subdirectories. If the text is found in any file, it will print the absolute path of that file. If the text is not found in any file, it will display a corresponding message.

For example, let's say you want to search for the text "apple" in the folder C:\Documents\Fruits. Here's what it would look like:

input text: apple path: C:\Documents\Fruits

The script will then proceed with the search and display any matching file paths or a message if the text is not found.

That's it! You can now use this code to search for a specific string in files within any folder on your computer. Remember to provide the correct search text and folder path for accurate results.


Tags:

Post a Comment

0Comments

Post a Comment (0)