| 1234567891011121314151617181920212223242526272829303132 |
- import os
- def search_in_env_files(directory, search_str):
- """
- Scan a directory for .env files, search for a string, and output the file path and line number of matches.
- Args:
- directory (str): The directory to scan.
- search_str (str): The string to search for in .env files.
- """
- if not os.path.exists(directory):
- print(f"Directory '{directory}' does not exist!")
- return
- print(f"Searching for '{search_str}' in .env files under '{directory}'...\n")
- for root, _, files in os.walk(directory):
- for file in files:
- if file.endswith(".env"):
- file_path = os.path.join(root, file)
- try:
- with open(file_path, "r") as f:
- for line_number, line in enumerate(f, start=1):
- if search_str in line:
- print(f"Match found in {file_path} (Line {line_number}): {line.strip()}")
- except Exception as e:
- print(f"Error reading file {file_path}: {e}")
- # Example usage
- if __name__ == "__main__":
- directory_to_scan = input("Enter the directory to scan: ").strip()
- search_string = input("Enter the string to search for: ").strip()
- search_in_env_files(directory_to_scan, search_string)
|