#!/usr/bin/env python3
from pathlib import Path

# USER: Set your directory path here
directory = "/var/www/html/development/hemantabhusal/py_filename"

def rename_files(target_dir):
    count = 0
    
    for item in target_dir.iterdir():
        if item.is_dir():
            for file in item.iterdir():
                if file.is_file() and '_thumb.' in file.name:
                    new_name = file.name.replace('_thumb.', '.')
                    new_path = file.parent / new_name
                    
                    try:
                        file.rename(new_path)
                        print(f"Done: {file.name} to {new_name}")
                        count += 1
                    except Exception as e:
                        print(f"Failed: {file.name} - {e}")
    
    print(f"\nTotal files renamed: {count}")

if __name__ == "__main__":
    dir_path = Path(directory)
    
    if not dir_path.exists():
        print(f"Error: Directory '{dir_path}' does not exist")
        exit(1)
    
    if not dir_path.is_dir():
        print(f" Error: '{dir_path}' is not a directory")
        exit(1)
    
    rename_files(dir_path)
