import os import json import pathlib folder_to_create = '/home/TEMPLATE_FOLDER_JAV' element_to_sustitute = '/home/data23/andalucia/Productos_entregables' path_to_list = ['/home/data23/andalucia/Productos_entregables/bloque_1', '/home/data23/andalucia/Productos_entregables/bloque_2', '/home/data23/andalucia/Productos_entregables/bloque_3', '/home/data23/andalucia/Productos_entregables/bloque_4', '/home/data23/andalucia/Productos_entregables/bloque_5', '/home/data23/andalucia/Productos_entregables/bloque_6'] dict_to_show = {} for item in path_to_list: dict_to_show[item] = [] items_to_list = [item] for element in items_to_list: folders = list((pathlib.Path(element)).iterdir()) for folder in folders: if os.path.isdir(folder): items_to_list.append(folder) dict_to_show[item].append(str(folder)) #print(dict_to_show) with open('/home/copiar_carpetas_json.json', 'w') as json_file: json.dump(dict_to_show, json_file, indent=4) for key in dict_to_show.keys(): name_folder = key.replace(element_to_sustitute, folder_to_create) os.mkdir(name_folder) for folder in dict_to_show[key]: name_subfolder = folder.replace(element_to_sustitute, folder_to_create) os.mkdir(name_subfolder)