Fix errors and bugs in the given code, without changing its intended functionality. ONLY…
- User
Fix errors and bugs in the given code, without changing its intended functionality. ONLY return the fixed code and nothing else. Enclose the entire code in a single code block.
import os import shutil
Define the source and target directories
source_dir = '/content' target_dir = '/content/data' # Change this to your desired target directory
Define the categories and sets
categories = ['Pematangan_dev-1'] sets = ['train/pematangan', 'valid/pematangan']
Create the target directory structure
for set_name in sets: for category in categories: os.makedirs(os.path.join(target_dir, set_name, category), exist_ok=True)
Function to move files
def move_files(set_name, category): src_path = os.path.join(source_dir, category, set_name, 'images') dst_path = os.path.join(target_dir, set_name, category) for file_name in os.listdir(src_path): shutil.move(os.path.join(src_path, file_name), os.path.join(dst_path, file_name))
Move the files to the new structure
for set_name in sets: for category in categories: move_files(set_name, category)
print("Files have been successfully moved!")
- Assistant
import os import shutil # Define the source and target directories source_dir = '/content' target_dir = '/content/data' # Change this to your desired target directory # Define the categories and sets categories = ['Pematangan_dev-1'] sets = ['train/pematangan', 'valid/pematangan'] # Create the target directory structure for set_name in sets: for category in categories: os.makedirs(os.path.join(target_dir, set_name, category), exist_ok=True) # Function to move files def move_files(set_name, category): src_path = os.path.join(source_dir, set_name, category, 'images') dst_path = os.path.join(target_dir, set_name, category) if os.path.exists(src_path): for file_name in os.listdir(src_path): shutil.move(os.path.join(src_path, file_name), os.path.join(dst_path, file_name)) # Move the files to the new structure for set_name in sets: for category in categories: move_files(set_name, category) print("Files have been successfully moved!")