import os import glob def rename_files(directory): filelist = glob.glob(filemask) for filename in filelist: basename = os.path.basename(filename) directory = os.path.dirname(filename) file_parts = basename.split('_') filepath = os.path.join(directory, filename) file_parts[3] = file_parts[3].replace('SINGLE', '') #file_parts[3] = file_parts[3] + 'SINGLE' new_file_name = '_'.join(file_parts) new_file_path = os.path.join(directory, new_file_name) try: os.rename(filename, new_file_path) print(filename, new_file_path) except OSError as e: print(f"Error renaming file {new_file_path}: {e}") # Specify the directory containing the files directory = '/webdata/web/www/htdocs/data/geo/images/suvi-goes-16/animation_images' channel = '*_HE303SINGLE_*' #channel = '*_HE303_*' filemask = os.path.join(directory, channel) # Call the function to rename files rename_files(filemask)