How do I modify this script so it evenly distributes the files within "src_dirs" to the "disks ="? Right now the code parses through "/mnt/disk1/Backup/Music" then distributes it to all "disks" equally in a round robin way then goes on to do "/mnt/disk2/Backup/Music" next and then "/mnt/disk3/Backup/Music" and then "/mnt/disk4/Backup/Music".
But this ends up creating uneven amount of files across the destination locations. Because of the way the script goes through disk1 to disk4, disk1 ends up having the most files and disk4 with the least files.
import os
import shutil
# Define the paths of the disks
disks = ['/mnt/disk1/Backup/Music', '/mnt/disk2/Backup/Music', '/mnt/disk3/Backup/Music', '/mnt/disk4/Backup/Music']
# Define the paths of the source directories
src_dirs = ['/mnt/disk1/Backup/Music', '/mnt/disk2/Backup/Music', '/mnt/disk3/Backup/Music', '/mnt/disk4/Backup/Music']
# Define a variable to keep track of the current disk index
current_disk_index = 0
# Recursively walk through the source directories and move files
for src_dir in src_dirs:
for dirpath, dirnames, filenames in os.walk(src_dir):
for filename in filenames:
src_path = os.path.join(dirpath, filename)
# Get the next disk path in a round-robin fashion
current_disk = disks[current_disk_index]
current_disk_index = (current_disk_index + 1) % len(disks)
dst_path = os.path.join(current_disk, os.path.relpath(src_path, start=src_dir))
# Create the destination directory if it doesn't exist
if not os.path.exists(os.path.dirname(dst_path)):
os.makedirs(os.path.dirname(dst_path))
# Move the file to the destination directory
shutil.move(src_path, dst_path)
print("Moved {} to {}".format(src_path, dst_path))
print("Finished")