Home > Software engineering >  How to move files 1 at a time to multiple folders in python
How to move files 1 at a time to multiple folders in python

Time:10-29

just want your insight on what I can do since I'm lost for 2 days now. What I'm trying to do is to move a file from 1 folder to 5 folders 1 at a time. here's what I got so far.

import os
import shutil

#path
source = 'C:/Users/folder/Downloads/Files/test'

destination = ['C:/Users/folder/Downloads/Files/test1',
    'C:/Users/folder/Downloads/Files/test2',
    'C:/Users/folder/Downloads/Files/test3',
    'C:/Users/folder/Downloads/Files/tes',
    'C:/Users/folder/Downloads/Files/test5']




def countItems():
    global totalfiles
    global copiedfiles
    totalfiles = 0
    copiedfiles = 1
    for item in os.listdir(source):
        totalfiles  = 1

#get total items
countItems()
#get Destination
while(totalfiles != 0):
    for dst in destination:
        for items in os.listdir(source):
            s = os.path.join(source, items)
            d = os.path.join(dst, items)
            if os.path.isfile(d):
                checker = 'Copy of'
                filename, filext = os.path.splitext(items)
                finalF = checker   filename   filext
                newd = os.path.join(dst, finalF)
                os.rename(s, newd)
                countItems()
            else:
                shutil.move(s, d)
                countItems()

I'm trying to distribute all of the files evenly throughout the 5 destination folder.

CodePudding user response:

You have a lot of extra nonsense in there to manipulate the file name, but nothing that rotates through the destinations. This does what you ask:

import os
import shutil

source = 'C:/Users/folder/Downloads/Files/test'

destination = [
    'C:/Users/folder/Downloads/Files/test1',
    'C:/Users/folder/Downloads/Files/test2',
    'C:/Users/folder/Downloads/Files/test3',
    'C:/Users/folder/Downloads/Files/tes',
    'C:/Users/folder/Downloads/Files/test5']

for item in os.listdir(source):
    dst = destination.pop(0)
    s = os.path.join(source, item)
    d = os.path.join(dst, 'Copy of ' item)
    os.rename(s, d)
    destination.append( dst )

CodePudding user response:

In order to copy all the items of a directory to another directory in one go, you can make use of 2 built-in methods:

Common code snippet to both methods: (Replace 'USERNAME' with your username)
source = r"C:/Users/USERNAME/OneDrive/Desktop/Dev/Rough/Source/"

destinations = [
    r"C:/Users/USERNAME/OneDrive/Desktop/Dev/Rough/Destination_1/",
    r"C:\Users\USERNAME\OneDrive\Desktop\Dev\Rough\CLI_Dev\Destination_2",
    r"C:/Users/USERNAME/OneDrive/Desktop/Dev/Projects/Destination_3/",
    r"C:/Users/USERNAME/OneDrive/Personal/Destination_4/",
    r"C:/Users/USERNAME/OneDrive/Backups/Destination_5/"
]

Method 1: distutils.dir_util.copy_tree()

import distutils.dir_util as du

for dest in destinations:
    if not os.path.exists(dest): os.mkdir(dest)         # Create dir if doesn't exist
    du.copy_tree(source, dest)                          # Copies all items to another dir

Method 2: shutil.copytree()

import shutil

for dest in destinations:
    shutil.copytree(source, dest, dirs_exist_ok = True) # Copies all items to another dir

dirs_exist_ok ensures no error is thrown in case dir already exists

Make use of these ways and you don't have to copy each item one by one to each dir

  • Related