checkinhands.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import os
  2. import argparse
  3. def find_folders_missing_file(root_folder, filename_to_check):
  4. """
  5. Recursively searches a root folder and lists subfolders
  6. that do not contain a specific file.
  7. Args:
  8. root_folder (str): The path to the root folder to start searching from.
  9. filename_to_check (str): The name of the file to check for.
  10. Returns:
  11. list: A list of full absolute paths to folders missing the specified file.
  12. """
  13. missing_folders = []
  14. # Ensure the root folder path is absolute and exists
  15. abs_root_folder = os.path.abspath(root_folder)
  16. if not os.path.isdir(abs_root_folder):
  17. print(f"Error: Folder not found - {abs_root_folder}")
  18. return missing_folders
  19. # Walk through the directory tree
  20. # os.walk yields dirpath, dirnames, filenames for each directory
  21. for dirpath, dirnames, filenames in os.walk(abs_root_folder):
  22. # Construct the full path to the file we're looking for in the current directory
  23. file_path_to_check = os.path.join(dirpath, filename_to_check)
  24. # Check if the file exists at that path
  25. # os.path.exists returns False if the path doesn't exist or isn't a file
  26. if not os.path.exists(file_path_to_check) or os.path.isdir(file_path_to_check):
  27. # Add the *absolute* path of the folder to our list if the file is missing
  28. missing_folders.append(os.path.abspath(dirpath))
  29. return missing_folders
  30. if __name__ == "__main__":
  31. # Set up argument parsing to make the script flexible
  32. parser = argparse.ArgumentParser(
  33. description="Find folders within a directory tree that are missing a specific file (default: 'inhand-left.png')."
  34. )
  35. # Optional argument for the folder to search. Defaults to the current directory ('.')
  36. parser.add_argument(
  37. "root_folder",
  38. nargs="?", # Makes the argument optional
  39. default=".", # Default value if no argument is provided
  40. help="The root folder to start the search from. Defaults to the current directory.",
  41. )
  42. # Optional argument to specify a different filename to check for
  43. parser.add_argument(
  44. "--filename",
  45. default="inhand-left.png",
  46. help="The name of the file to check for (default: inhand-left.png).",
  47. )
  48. # Parse the command-line arguments
  49. args = parser.parse_args()
  50. target_folder = args.root_folder
  51. file_to_find = args.filename
  52. abs_target_folder = os.path.abspath(target_folder) # Get absolute path for clarity
  53. print(f"\nSearching for folders missing '{file_to_find}'")
  54. print(f"Starting in: '{abs_target_folder}'\n")
  55. # Run the search function
  56. folders_without_file = find_folders_missing_file(target_folder, file_to_find)
  57. # Print the results
  58. if folders_without_file:
  59. print("----------------------------------------")
  60. print("Folders missing the file:")
  61. print("----------------------------------------")
  62. for folder in folders_without_file:
  63. print(folder)
  64. print("----------------------------------------")
  65. else:
  66. print(
  67. f"✅ All folders checked within '{abs_target_folder}' contain the file '{file_to_find}'."
  68. )
  69. print("\nSearch complete.")