How to create safely a nested directory
Sun Oct 09 2022 17:23:30 GMT+0000 (Coordinated Universal Time)
Saved by
@DiegoEraso
# remember those lines
#from pathlib import Path
#Path("/root/dirA/dirB").mkdir(parents=True, exist_ok=True)
import os
# funcion que recibe un cadena y crea un direcorio que recibe como nombre dicha cadena
def make_directories(file):
file = str(file)
file_path = os.path.join(base_dir, file)
if os.path.exists(file_path) == False:
os.mkdir(file_path)
print('Directorio creado :' + file_path)
else:
print('El directorio: ' + file_path + ' ya existe!!!')
from pathlib import Path
# creacion de directorios en 2 niveles
def make_2_level_directories(root_folder_name, directories_to_make=[]):
new_path_dir= []
new_path_root = file_path = os.path.join(base_dir, root_folder_name)
Path(new_path_root).mkdir(parents=True, exist_ok=True)
if len(directories_to_make) == 0:
return print("No hay subdirectorios para crear")
for file in directories_to_make :
new_path = os.path.join(root_folder_name,file)
new_path_dir.append(new_path)
for file in new_path_dir:
make_directories(file) # no olvidar cambiar el directorio base
content_copyCOPY
https://www.programiz.com/python-programming/examples/create-nested-directories#:~:text=For%20python%203.2%20and%20above,makedirs%20.&text=Using%20method%20makedirs()%20from,directory%20we%20wanted%20to%20create.
Comments