HugeCSV
HugeCSV

Reputation: 3

How to move same numbers of files in different folders using Python

I have a folder with a lot of csv files I can manipulate them quite easily with for file in Path(folder).glob('*.csv'): and pandas where folder is the path of the folder.

But now I have to move those files in folders named like "folder1", "folder2", "folder3"...

I want there to be the same number of files in each folder plus or minus 1 file because if the number of files does not divide the number of folders there will be 1 more file in some.

I can't do it using .startswith because I have like 20 folders and files don't begin like I want.

Thanks in advance and sorry for my bad english.

Upvotes: 0

Views: 164

Answers (1)

Michael Butscher
Michael Butscher

Reputation: 10969

A simple solution can be this function which returns a list of file counts, one for each folder:

def d(numfiles, numfolders):
    result = [numfiles // numfolders] * numfolders
    for i in range(numfiles % numfolders):
        result[i] += 1
    return result

print(d(10, 3))

Prints:

[4, 3, 3]

Upvotes: 1

Related Questions