python split files into even sets of folders

Solutions on MaxInterview for python split files into even sets of folders by the best coders in the world

showing results for - "python split files into even sets of folders"
Alessandra
01 May 2019
1import os       # Used to do path manipulations
2import shutil   # Used to copy files
3
4# Taken from https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
5def chunks(lst:list, n:int) -> list:
6    """Yield successive n-sized chunks from lst."""
7    for i in range(0, len(lst), n):
8        yield lst[i:i + n]
9
10def get_abspath_files_in_directory(directory:str) -> list:
11    """Takes in a directory and returns the abspath of all the files in the directory as a list
12
13    Parameters
14    ----------
15    directory : str
16        The directory to get the abspaths for
17
18    Returns
19    -------
20    list
21        A list of the abspaths of all the files in the directory
22    """
23    return [os.path.abspath(os.path.join(directory,path)) for path in  os.listdir(directory)]
24
25
26def split_to_subdirectories(file_paths:list, amount_per_folder:int):
27    """Take in a list of file absolute paths, and copy them to folders
28
29    Parameters
30    ----------
31    file_paths : list
32        The list of abspaths to the file folders
33
34    amount_per_folder : int
35        The amount of files per folder to split the files into
36    """
37    file_paths = chunks(file_paths, amount_per_folder)
38
39    for index, chunk in enumerate(file_paths):
40        os.mkdir(str(index)) # Create a folder with a name of the current iteration index
41        for file_path in chunk:
42            file_name = file_path.split(os.sep)[-1]
43            shutil.copy(file_path, os.path.join(str(index), file_name))
44
45if __name__ == "__main__":
46    file_paths = get_abspath_files_in_directory("original_folder") # Replace "original_folder" with the directory where your files are stored
47    split_to_subdirectories(file_paths, 20)
48
similar questions