File size: 4,666 Bytes
58e7e8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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)