import os # Define the absolute path to the font_files directory FONT_FILES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'font_files') # find all files that are otfs or ttfs at the root of the font_files directory def handle_orphans(font_files_dir=FONT_FILES_DIR): FONT_FILES = [file for file in os.listdir(FONT_FILES_DIR) if file.endswith('.otf') or file.endswith('.ttf')] print(FONT_FILES) for file in FONT_FILES: #create a directory for the file new_dir_path = os.path.join(FONT_FILES_DIR, file.split('.')[0]) if not os.path.exists(new_dir_path): os.mkdir(new_dir_path) print(f"Created directory: {new_dir_path}") #move the file to the directory new_file_path = os.path.join(new_dir_path, file) os.rename(os.path.join(FONT_FILES_DIR, file), new_file_path) print(f"Moved {file} to {new_file_path}") if __name__ == '__main__': handle_orphans()