import os # Path to the folder containing the images and text files folder_path = "kohya_finetune_data" # List of valid image extensions image_extensions = ('.png', '.jpg', '.jpeg') # Get lists of image and text files in the folder image_files = [f for f in os.listdir(folder_path) if f.lower().endswith(image_extensions)] text_files = [f for f in os.listdir(folder_path) if f.endswith('.txt')] # Create sets of base filenames without extensions image_basenames = {os.path.splitext(f)[0] for f in image_files} text_basenames = {os.path.splitext(f)[0] for f in text_files} # Check for image files without corresponding text files for image_file in image_files: base_name = os.path.splitext(image_file)[0] if base_name not in text_basenames: os.remove(os.path.join(folder_path, image_file)) print(f'Removed image file without corresponding text file: {image_file}') # Check for text files without corresponding image files for text_file in text_files: base_name = os.path.splitext(text_file)[0] if base_name not in image_basenames: os.remove(os.path.join(folder_path, text_file)) print(f'Removed text file without corresponding image file: {text_file}')