Search code examples
pythonlistcopymoveshutil

Copying files from source folder to different destination folders


I am trying to copy files indicated in the Sources list into the destination list. I only want Source[1] to go to Destination[1] then Source [2] to Destination [2] and so on and so forth.

Currently the program will copy all of the sources list files into all of the destination folders instead of just the first source file into the first destination folder.

Thank you!

import os
import shutil
import glob


#Move Program

Sources = [r"C:\Users\simon.rhee\Desktop\Met 201707\GR_MonthlySummary_EDF_DesertHarvest_2017-07.pdf",
       r"C:\Users\simon.rhee\Desktop\Met 201707\GR_MonthlySummary_EDF_Goodranch_2017-07.pdf",
       r"C:\Users\simon.rhee\Desktop\Met 201707\GR_MonthlySummary_EDF_Jackson_2017-07.pdf",
       r"C:\Users\simon.rhee\Desktop\Met 201707\GR_MonthlySummary_EDF_Lodi_2017-07.pdf",
       r"C:\Users\simon.rhee\Desktop\Met 201707\GR_MonthlySummary_EDF_Moapa_2017-07.pdf",
       r"C:\Users\simon.rhee\Desktop\Met 201707\GR_MonthlySummary_EDF_Ponderosa_2017-07.pdf",
       r"C:\Users\simon.rhee\Desktop\Met 201707\GR_MonthlySummary_EDF_UtahSolar1_2017-07.pdf"]



Destinations = [r"G:\MetData\Solar\USA_West\7005 - Desert Harvest_16\1_Asset Management\GroundWorks\1_Monthly Reports",
            r"G:\MetData\Solar\USA_Central\4006_Goodranch\1_Asset Management\Reports",
            r"G:\MetData\Solar\USA_East\8002_Jackson\1_Asset Management\GroundWorks\1_Monthly Reports",
            r"G:\MetData\Solar\USA_Central\4005_Lodi\1_Asset Management\Reports",
            r"G:\MetData\Solar\USA_West\7013_Moapa\1_Asset Management\Reports",
            r"G:\MetData\Solar\USA_West\7012_Ponderosa\1_Asset Management\Groundworks\1_Monthly Reports",
            r"G:\MetData\Solar\USA_West\7011_Utah_Solar_One\1_Asset Management\Reports"]




i = 1
for x in Sources:
    for y in Destinations:
        shutil.copy2(x,y)
        print ("Program",i,"Complete")
        i += 1

Solution

  • Two fors will give you exactly that behavior. As it is now, you take the first element in Sources and put it to x, then with that same x you iterate through all elements in destinations and putting them to y and performing the copy. So if the first array has 5 elements and the second 5 as well the copy runs 25 times.

    Instead you need to have only 1 for which will take an element from both arrays at a time, like so:

    for x in range(len(Sources)):
        shutil.copy2(Sources[x],Destinations[x])
    
    print ("Program",i,"Complete")