Necesitaba algo un poco más genérico, es decir, mover todos los archivos de todas las carpetas [sub] + a la carpeta raíz.
Por ejemplo comenzar con:
root_folder
|----test1.txt
|----1
|----test2.txt
|----2
|----test3.txt
y terminar con:
root_folder
|----test1.txt
|----test2.txt
|----test3.txt
Una función recursiva rápida hace el truco:
import os, shutil, sys
def move_to_root_folder(root_path, cur_path):
for filename in os.listdir(cur_path):
if os.path.isfile(os.path.join(cur_path, filename)):
shutil.move(os.path.join(cur_path, filename), os.path.join(root_path, filename))
elif os.path.isdir(os.path.join(cur_path, filename)):
move_to_root_folder(root_path, os.path.join(cur_path, filename))
else:
sys.exit("Should never reach here.")
# remove empty folders
if cur_path != root_path:
os.rmdir(cur_path)
lo normal es que llamarlo con el mismo argumento para root_path
y cur_path
, por ejemplo move_to_root_folder(os.getcwd(),os.getcwd())
si quieres probarlo en el entorno python.
¿Qué has probado? La documentación 'shutil.move (src, dst)' dice "Moviendo recursivamente un archivo o directorio (src) a otra ubicación (dst)", por lo que debería funcionar. – jcollado