How to Divide Folder? in python

1

Good afternoon.  I have a folder (directory) with many files (more than 25000) and I need to divide that folder into sub-folders that contain 500 files each.

-the criteria for dividing the files can be by alphabetical order, by size, by date of creation, randomly etc. -folios created can have any name.

Does anyone have an idea how to do it?

I hope you have understood the question.

    
asked by Carlos Catalán 17.11.2016 в 16:10
source

2 answers

1

Try this

import os

path = "yourpath"
count = 0
factor = 500 # numero de archivos por carpeta
curren_dir = ""
for root, dirs, files in os.walk(path):
    for file in files:
        if count % factor == 0:
            curren_dir = "{path}/subdirectory{count}".format(path=path,
                                                             count=int(count / factor))
            os.mkdir(curren_dir)
        os.rename("{path}/{file}".format(path=path, file=file),
                  "{path}/{file}".format(path=curren_dir, file=file))
        count += 1
    
answered by 17.11.2016 / 16:43
source
1

This code can help you to start, you have to mix it with your criteria. The small example creates two folders (folder2 and Example) this is done in the directory where the python file is, and the example folder moves it into "carpetanueva2" for the case we moved the folder but you can put the file you want to move and will move it.

# -*- coding: utf-8 -*-
import os, shutil

os.system("mkdir carpetanueva2") #Crea una carpeta llamada 'carpetanueva' en el actual directorio
os.system("mkdir Ejemplo") #Crea una carpeta llamada 'carpetanueva' en el actual directorio

shutil.move("Ejemplo", "./carpetanueva2") #Movera la carpeta "Ejemplo" dentro de "carpetanueva2"

Greetings, if there are any questions, please comment and I can continue to help you.

    
answered by 17.11.2016 в 16:42