Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python split files into even sets of folders

import os       # Used to do path manipulations
import shutil   # Used to copy files

# Taken from https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
def chunks(lst:list, n:int) -> list:
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

def get_abspath_files_in_directory(directory:str) -> list:
    """Takes in a directory and returns the abspath of all the files in the directory as a list

    Parameters
    ----------
    directory : str
        The directory to get the abspaths for

    Returns
    -------
    list
        A list of the abspaths of all the files in the directory
    """
    return [os.path.abspath(os.path.join(directory,path)) for path in  os.listdir(directory)]


def split_to_subdirectories(file_paths:list, amount_per_folder:int):
    """Take in a list of file absolute paths, and copy them to folders

    Parameters
    ----------
    file_paths : list
        The list of abspaths to the file folders

    amount_per_folder : int
        The amount of files per folder to split the files into
    """
    file_paths = chunks(file_paths, amount_per_folder)

    for index, chunk in enumerate(file_paths):
        os.mkdir(str(index)) # Create a folder with a name of the current iteration index
        for file_path in chunk:
            file_name = file_path.split(os.sep)[-1]
            shutil.copy(file_path, os.path.join(str(index), file_name))

if __name__ == "__main__":
    file_paths = get_abspath_files_in_directory("original_folder") # Replace "original_folder" with the directory where your files are stored
    split_to_subdirectories(file_paths, 20)
Comment

PREVIOUS NEXT
Code Example
Python :: pandas show all columns 
Python :: How to make boxplot using seaborne 
Python :: how to make a series in python alternating between + and - 
Python :: hebrew range 
Python :: mechanize python #8 
Python :: step out pdb python 
Python :: deine dict with same values 
Python :: python profile is not defined line_profiler 
Python :: Local to ISO 8601 without microsecond: 
Python :: how to get the original start_url in scrapy 
Python :: accumulate sum of elements in list 
Python :: metasploit in python 
Python :: python anywhere just says hello from flask 
Python :: How to avoit print() to go to newline each time 
Python :: python nltk lookup error Resource omw-1.4 not found. 
Python :: python nmap api functionality 
Python :: program to add two numbers in python 
Python :: pyttsx3 ichanging voices 
Python :: html in nested structure 
Python :: Using Python Permutations function on a String with extra parameter 
Python :: en python quand on utilise = et== 
Python :: print using multiply only 
Python :: selenium emojis 
Python :: find all html files in a current directory using regular expression in python 
Python :: list expression inside bracket python 
Python :: os.path.join not working 
Python :: Python NumPy asanyarray Function Example Scalar to an array 
Python :: inverrt heatmap cmap 
Python :: maximaze window in tkinter 
Python :: NumPy bitwise_and Example When inputs are Boolean 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =