Transfer files from sub-folder to main folder in python

5

I have enough folders that in turn have their respective sub-folders, inside one of those sub-folders there is a subfolder_1_2, subfolder_2_2, subfolder_3_2 that contains files for example:

 carpeta
    │── Subcarpeta_Principal1
    │   │──Subcarpeta_1_1
    │   │   │── Subcarpeta_1_2
    │   │   │   │──Archivo.txt
    │   │   │   │──Archivo.txt
    │── Subcarpeta_Principal2
    │   │──Subcarpeta_2_1
    │   │   │── Subcarpeta_2_2
    │   │   │   │──Archivo.txt
    │   │   │   │──Archivo.txt
    │── Subcarpeta_Principal3
    │   │──Subcarpeta_3_1
    │   │   │── Subcarpeta_3_2
    │   │   │   │──Archivo.txt
    │   │   │   │──Archivo.txt

And what I want to know is if it is possible to move those files to their respective subfolders: Subfolder_Principal1, Subfolder_Principal2, Subfolder_Principal3 , as shown in the example:

 carpeta
    │── Subcarpeta_Principal1
    │   │   │   │──Archivo.txt
    │   │   │   │──Archivo.txt
    │   │──Subcarpeta_1_1
    │   │   │── Subcarpeta_1_2
    │── Subcarpeta_Principal2
    │   │   │   │──Archivo.txt
    │   │   │   │──Archivo.txt
    │   │──Subcarpeta_2_1
    │   │   │── Subcarpeta_2_2
    │── Subcarpeta_Principal3
    │   │   │   │──Archivo.txt
    │   │   │   │──Archivo.txt
    │   │──Subcarpeta_3_1
    │   │   │── Subcarpeta_3_2

How could I do it?

    
asked by Armando 19.01.2016 в 21:44
source

1 answer

4

We're going to need some imports:

from os import listdir
from os.path import isfile, join, isdir
from shutil import move

Let's get a list of folders located in a base folder:

carpetaBase='/home/test/py/t'
carpetas = [ c for c in listdir(carpetaBase) if isdir(join(carpetaBase,c)) ]

Those folders are the main folders to which we want to move all the files that are in their subfolders.
For each main folder we get a list of the subfolders that are immediately contained in them.

for c in carpetas:
  carpeta = join(carpetaBase, c)
  subCarpetas = [ sc for sc in listdir(carpeta) if isdir(join(carpeta,sc)) ]

For each one of those subCarpets we want to move the files in them contained to their main folder:

  for sc in subCarpetas:
    subCarpeta = join( carpeta, sc )
    moverFicherosEnSubcarpetas( subCarpeta, carpeta )

Now we need to define moverFicherosEnSubcarpetas , which must move the files contained in the folder of the first parameter to the folder of the second parameter.

def moverFicherosEnSubcarpetas( carpeta, destino ):
  # Mover ficheros
  ficheros = [ f for f in listdir(carpeta) if isfile(join(carpeta,f)) ]
  for f in ficheros:
    move( join(carpeta,f), destino)

And with this almost everything is done. But one thing is missing. So far we have moved the files from the subfolders to their main folders. But what about the subfolder files in the subfolders? And those of the subfolders of the subfolders of the subfolders ?, etc ...

We can move them called to moverFicherosEnSubcarpetas recursively:

  carpetas = [ c for c in listdir(carpeta) if isdir(join(carpeta,c)) ]
  for c in carpetas:
    moverFicherosEnSubcarpetas( join(carpeta,c) , destino)

The code as a whole remains:

#!/usr/bin/python
from os import listdir
from os.path import isfile, join, isdir
from shutil import move

def moverFicherosEnSubcarpetas( carpeta, destino ):
  # Mover ficheros
  ficheros = [ f for f in listdir(carpeta) if isfile(join(carpeta,f)) ]
  for f in ficheros:
    move( join(carpeta,f), destino)
  # Llamar recursivamente a subcarpetas
  carpetas = [ c for c in listdir(carpeta) if isdir(join(carpeta,c)) ]
  for c in carpetas:
    moverFicherosEnSubcarpetas( join(carpeta,c) , destino)

carpetaBase='/home/test/py/t'
carpetas = [ c for c in listdir(carpetaBase) if isdir(join(carpetaBase,c)) ]
for c in carpetas:
  carpeta = join(carpetaBase, c)
  subCarpetas = [ sc for sc in listdir(carpeta) if isdir(join(carpeta,sc)) ]
  for sc in subCarpetas:
    subCarpeta = join( carpeta, sc )
    moverFicherosEnSubcarpetas( subCarpeta, carpeta )
    
answered by 20.01.2016 / 08:47
source