I have a code snippet that works (or should've) by sorting graphs contained into separate parameter folders, based on the dataframe that I've extracted the graphs from. It sort of works: instead of sorting each folder's contents into the parameter folders, it overwrites the first folder's content into the parameter folders, numbering to the amount of dataframe folders are there. How should I go about fixing this? Thank you in advance.
#@title Sorting output to parameter folders - Train
import multiprocessing
import os
import shutil
parameter_folders = {
"Acceleration Normal": "acceleration_normal_train_graphs",
"Altitude": "altitude_train_graphs",
"Airspeed": "airspeed_train_graphs",
"Altitude Radio": "altitude_radio_train_graphs",
"Heading": "heading_train_graphs",
"Heading Mag-True": "heading_mag-true_train_graphs",
"Pitch": "pitch_train_graphs",
"Roll": "roll_train_graphs"
}
def move_files(parameter_name, source_folder, destination_folder, df_idx):
# Create the parameter-specific folder if it doesn't exist
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
# Move the graphs to the parameter-specific folder, sorted by file name
for file_name in sorted(os.listdir(source_folder)):
if file_name.endswith(f"{parameter_name}.jpg"):
source_path = os.path.join(source_folder, file_name)
new_file_name = f"{parameter_name}_{df_idx}_train.jpg"
destination_path = os.path.join(destination_folder, new_file_name)
shutil.move(source_path, destination_path)
else:
print(f"Whoops! File '{file_name}' in '{source_folder}' cannot be sorted!")
def main():
folder_list = [folder_name for folder_name in os.listdir(os.path.join(whole_directory, "train graphs"))]
pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
jobs = []
for df_idx in range(len(folder_list)):
source_folder = os.path.join(whole_directory, "train graphs", f"train_graphs_df_{df_idx}")
if os.path.exists(source_folder):
for parameter_name, destination_folder in parameter_folders.items():
destination_folder = os.path.join(whole_directory, "train graphs", destination_folder)
job = pool.apply_async(move_files, (parameter_name, source_folder, destination_folder, df_idx))
jobs.append(job)
pool.close()
pool.join()
if __name__ == "__main__":
main()