examine_files.py 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. import os
  2. def search_in_env_files(directory, search_str):
  3. """
  4. Scan a directory for .env files, search for a string, and output the file path and line number of matches.
  5. Args:
  6. directory (str): The directory to scan.
  7. search_str (str): The string to search for in .env files.
  8. """
  9. if not os.path.exists(directory):
  10. print(f"Directory '{directory}' does not exist!")
  11. return
  12. print(f"Searching for '{search_str}' in .env files under '{directory}'...\n")
  13. for root, _, files in os.walk(directory):
  14. for file in files:
  15. if file.endswith(".env"):
  16. file_path = os.path.join(root, file)
  17. try:
  18. with open(file_path, "r") as f:
  19. for line_number, line in enumerate(f, start=1):
  20. if search_str in line:
  21. print(f"Match found in {file_path} (Line {line_number}): {line.strip()}")
  22. except Exception as e:
  23. print(f"Error reading file {file_path}: {e}")
  24. # Example usage
  25. if __name__ == "__main__":
  26. directory_to_scan = input("Enter the directory to scan: ").strip()
  27. search_string = input("Enter the string to search for: ").strip()
  28. search_in_env_files(directory_to_scan, search_string)