Skip to content

Instantly share code, notes, and snippets.

@phinate
Created March 10, 2023 14:56
Show Gist options
  • Save phinate/e4438ef726ea0874b6826b42a2f97abc to your computer and use it in GitHub Desktop.
Save phinate/e4438ef726ea0874b6826b42a2f97abc to your computer and use it in GitHub Desktop.
sorts out the folders that the boss rc505 makes with more semantic names and better grouping based on a csv file
import polars as pl
import os
import shutil
q = (
pl.scan_csv("l.csv")
)
df = q.collect()
strings = [f'[{a:.2g}]' for a in df["song potential"]]
df = df.with_columns(pl.Series("song potential", strings))
name_dict = {a:b for a,b in zip(df["Number"], df.select(pl.concat_str(["song potential", "Name"], "-").alias("filename"))["filename"])}
# Set the path to the directory containing the folders
path = 'path/to/WAVE/'
# Get a list of all the folders in the directory
folders = [f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]
# Iterate over the folders and move the .wav files to the correct directory
for folder in folders:
files = os.listdir(os.path.join(path, folder))
for file in files:
if file.lower().endswith('.wav'):
file_parts = file.split('_')
new_folder = os.path.join(path, file_parts[0])
if not os.path.exists(new_folder):
os.makedirs(new_folder)
shutil.move(os.path.join(path, folder, file), os.path.join(new_folder, file))
# Get a list of all the folders in the directory
folders = [f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]
# Iterate over the folders and rename them according to the dictionary
for folder in folders:
old_name = folder
try:
int(folder)
new_name = name_dict.get(int(folder), None)
except ValueError as e:
continue
if new_name is not None:
# Check if the folder is empty
if not os.listdir(os.path.join(path, old_name)):
os.rename(os.path.join(path, old_name), os.path.join(path, new_name))
else:
# If the folder is not empty, move its contents to the new folder and delete the old folder
shutil.move(os.path.join(path, old_name), os.path.join(path, new_name))
print("done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment