import os import time from fileinput import filename bad_phrases = [' Free', 'TBJ ', ' - Zarma Type', ' Only', ' Personal use', ' PERSONAL USE', ' SVG', ' Free Non-Commercial Use', ' Free for Personal Use', ' Free for personal use', ' Personal Use', ' Personl Use Only', ' Personl Use', ' Free To Try Personal Use Only', ' - Personal Use Only', ' - DCU', ' - Demo', ' - Free for Personal Use', ' - Free for personal use', ' - Free for personal use only', ' Demo', ' DCU', ' To Try', ' - Free Personal Use Only', ' - Free Personal Use', ' - Personal use Only', ' -', "'", 'Non-Commercial Use', ] def remove_ds_store_files(path): """ Recursively removes .DS_Store files within the given path. Args: path (str): The path to the directory to be checked and cleaned. Returns: None """ for root, dirs, files in os.walk(path): for file in files: if file == '.DS_Store': file_path = os.path.join(root, file) os.remove(file_path) print(f"Removed .DS_Store file: {file_path}") def remove_empty_folders(path): """ Recursively removes empty directories within the given path, except for .git and .env directories and their subdirectories. Args: path (str): The path to the directory to be checked and cleaned. Returns: None """ for root, dirs, files in os.walk(path, topdown=False): for dir in dirs: dir_path = os.path.join(root, dir) if '.git' in dir_path.split(os.sep) or '.env' in dir_path.split(os.sep): continue if not os.listdir(dir_path): os.rmdir(dir_path) print(f"Removed empty folder: {dir_path}") # Usage def remove_bad_phrases(path): """ Recursively removes bad phrases from the names of directories at the root of the given path. Args: path (str): The path to the directory to be checked and cleaned. Returns: None """ while True: removed_any = False for root, dirs, files in os.walk(path): for dir in dirs: dir_path = os.path.join(root, dir) for phrase in bad_phrases: if phrase in dir: new_dir = dir.replace(phrase, '') new_dir_path = os.path.join(root, new_dir) os.rename(dir_path, new_dir_path) print(f"Renamed directory: {dir_path} -> {new_dir_path}") removed_any = True break # Exit the loop after renaming to avoid modifying the same directory multiple times if not removed_any: break # time.sleep(1) # Delay to allow the file system to settle def collect_orphaned_fonts(path): """ Collects orphaned font files at the root of the given path. Args: path (str): The path to the directory to be checked for orphaned font files. Returns: list: A list of orphaned font files. """ orphaned_fonts = [] for file in os.listdir(path): file_path = os.path.join(path, file) if os.path.isfile(file_path) and (file.endswith('.otf') or file.endswith('.ttf')): # Create a directory for the orphaned font orphan_folder = os.path.join(path, os.path.splitext(file)[0]) if not os.path.exists(orphan_folder): os.mkdir(orphan_folder) # Move the font file into the new directory new_path = os.path.join(orphan_folder, file) os.rename(file_path, new_path) orphaned_fonts.append(new_path) print(f"Moved {file_path} to {new_path}") return orphaned_fonts def lowercase_all_the_folders(root_path): """ Lowercases all the folders in the root path, except those starting with a period. Args: root_path (str): The path to the directory to be checked and cleaned. Returns: None """ for dir in os.listdir(root_path): dir_path = os.path.join(root_path, dir) if os.path.isdir(dir_path): if dir.startswith('.'): continue new_dir_path = os.path.join(root_path, dir.lower()) os.rename(dir_path, new_dir_path) print(f"Renamed directory: {dir_path} -> {new_dir_path}") def add_font_folders_to_each(root_path): for dir in os.listdir(root_path): dir_path = os.path.join(root_path, dir) if os.path.isdir(dir_path): if dir.startswith('.'): continue #get recursive list of files in the directory files = os.listdir(dir_path) print(files) for file in files: # if file extension is otf, create a folder named oft if it doesn't exist, and move the file there if file.endswith('.otf'): new_dir_path = os.path.join(dir_path, 'otf') if not os.path.exists(new_dir_path): os.mkdir(new_dir_path) new_file_path = os.path.join(new_dir_path, file) os.rename(os.path.join(dir_path, file), new_file_path) print(f"Moved {file} to {new_file_path}") # if file extension is ttf, create a folder named oft if it doesn't exist, and move the file there elif file.endswith('.ttf'): new_dir_path = os.path.join(dir_path, 'ttf') if not os.path.exists(new_dir_path): os.mkdir(new_dir_path) new_file_path = os.path.join(new_dir_path, file) os.rename(os.path.join(dir_path, file), new_file_path) print(f"Moved {file} to {new_file_path}") else: # if file is not a font file,move it to a folder called "other" new_dir_path = os.path.join(dir_path, 'other') if not os.path.exists(new_dir_path): os.mkdir(new_dir_path) new_file_path = os.path.join(new_dir_path, file) os.rename(os.path.join(dir_path, file), new_file_path) if __name__ == '__main__': remove_ds_store_files('/Users/fishy/custom_fonts/') remove_empty_folders('/Users/fishy/custom_fonts/') remove_bad_phrases('/Users/fishy/custom_fonts/') collect_orphaned_fonts('/Users/fishy/custom_fonts/') lowercase_all_the_folders('/Users/fishy/custom_fonts/') add_font_folders_to_each('/Users/fishy/custom_fonts/') remove_ds_store_files('/Users/fishy/custom_fonts/') remove_empty_folders('/Users/fishy/custom_fonts/')