| import os | |
| import shutil | |
| # def reorganize_metaworld_dataset(dataset_dir): | |
| # """ | |
| # Reorganize the metaworld_dataset by moving all subfolders from corner, corner2, and corner3 | |
| # to their respective parent task folders and renaming them sequentially. | |
| # Args: | |
| # dataset_dir (str): Path to the metaworld_dataset directory. | |
| # """ | |
| # # Iterate over each task folder in the dataset directory | |
| # for task_folder in os.listdir(dataset_dir): | |
| # task_path = os.path.join(dataset_dir, task_folder) | |
| # if os.path.isdir(task_path): | |
| # # List to collect all subfolders from corner, corner2, and corner3 | |
| # all_subfolders = [] | |
| # # Define paths to corner, corner2, and corner3 directories | |
| # corner_dirs = [os.path.join(task_path, f'corner{i}') for i in ['', '2', '3']] | |
| # for corner_dir in corner_dirs: | |
| # if os.path.exists(corner_dir): | |
| # # Collect all subfolders in the corner directory | |
| # for subfolder in os.listdir(corner_dir): | |
| # subfolder_path = os.path.join(corner_dir, subfolder) | |
| # if os.path.isdir(subfolder_path): | |
| # all_subfolders.append(subfolder_path) | |
| # # Move and rename collected subfolders | |
| # for index, subfolder_path in enumerate(all_subfolders): | |
| # new_subfolder_name = f"{index}" | |
| # new_subfolder_path = os.path.join(task_path, new_subfolder_name) | |
| # shutil.move(subfolder_path, new_subfolder_path) | |
| # print(f"Moved and renamed: {subfolder_path} -> {new_subfolder_path}") | |
| # for corner_dir in corner_dirs: | |
| # if os.path.exists(corner_dir): | |
| # # Remove the now-empty corner directory | |
| # os.rmdir(corner_dir) | |
| # print(f"Removed directory: {corner_dir}") | |
| # # Example usage | |
| # dataset_directory = "/home/lei/Documents/forfun/multi-task-tcc/experiments/metaworld_dataset" | |
| # reorganize_metaworld_dataset(dataset_directory) | |
| # def rename_images_in_folders(parent_dir): | |
| # """ | |
| # Rename image files in each numbered folder within the parent directory by removing the leading 0 | |
| # from filenames that start with 0. | |
| # Args: | |
| # parent_dir (str): Path to the parent directory containing numbered folders. | |
| # """ | |
| # # Iterate over each numbered folder in the parent directory | |
| # for task in os.listdir(parent_dir): | |
| # task_path = os.path.join(parent_dir, task) | |
| # for folder in os.listdir(task_path): | |
| # folder_path = os.path.join(task_path, folder) | |
| # if os.path.isdir(folder_path): | |
| # # Rename images within the folder | |
| # for img_filename in os.listdir(folder_path): | |
| # img_path = os.path.join(folder_path, img_filename) | |
| # if os.path.isfile(img_path) and img_filename.endswith('.png') and img_filename.startswith('0'): | |
| # new_img_name = img_filename.lstrip('0') | |
| # new_img_path = os.path.join(folder_path, new_img_name) | |
| # os.rename(img_path, new_img_path) | |
| # print(f"Renamed image: {img_path} -> {new_img_path}") | |
| def rename_images_in_folders(parent_dir): | |
| """ | |
| Rename image files in each numbered folder within the parent directory by removing the leading 0 | |
| from filenames that start with 0. | |
| Args: | |
| parent_dir (str): Path to the parent directory containing numbered folders. | |
| """ | |
| # Iterate over each numbered folder in the parent directory | |
| for task in os.listdir(parent_dir): | |
| task_path = os.path.join(parent_dir, task) | |
| for folder in os.listdir(task_path): | |
| folder_path = os.path.join(task_path, folder) | |
| if os.path.isdir(folder_path): | |
| # Rename images within the folder | |
| for img_filename in os.listdir(folder_path): | |
| img_path = os.path.join(folder_path, img_filename) | |
| if os.path.isfile(img_path) and img_filename.endswith('.png') and img_filename.startswith('.'): | |
| new_img_name = '0.png' | |
| new_img_path = os.path.join(folder_path, new_img_name) | |
| os.rename(img_path, new_img_path) | |
| print(f"Renamed image: {img_path} -> {new_img_path}") | |
| # Example usage | |
| parent_directory = "/home/lei/Documents/forfun/multi-task-tcc/experiments/metaworld_dataset" | |
| rename_images_in_folders(parent_directory) | |