1

I want to copy an entire directory or just files from a directory to two or more directories at the same time. I'm struggling with the syntax to add more directories for the destination, so the copy is done at the same time (as I'll run a cron job).

Everything works fine if I have one single directory, however I need to add two or more.

The last line of code is:

shutil.copy(full_file_name, r'S:\\A')

I want to add more destination folders after S:\

(this is for Win machines)

Thanks for help!

3
  • I think you'll have to add one shutil.copy() for each destination directory. Commented Sep 3, 2016 at 13:39
  • There is no way to copy to two destinations "at the same time". Almost nothing in a computer program happens precisely "at the same time", I guess may be asking the wrong thing. Do you mean that the files in both destinations should be created syncronously or that you want to execute just one line of code to do two things? Commented Sep 3, 2016 at 14:47
  • Yes - sorry I did not mean at the "exact same time", but in the same (or few) line of code. Thanks. Commented Sep 3, 2016 at 20:19

3 Answers 3

1

Why not just wrap in a loop:

destinations = [r'S:\\A', r'S:\\B', r'S:\\C']
for dest in destinations:
    shutil.copy(full_file_name, dest)
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to copy the files at the same time, you should use multiprocessing. In this sample, we have two files file1.txt and file2.txt and we will copy them to c:\temp\0 and c:\temp\1.

import multiprocessing
import shutil

def main():
    orgs = ['c:\\temp\\file1.txt']
    dests = ['c:\\temp\\0\\', 'c:\\temp\\1\\'] 
    num_cores = multiprocessing.cpu_count()
    p = multiprocessing.Pool(num_cores)
    operations =  [(x,y) for x in orgs for y in dests]
    p.starmap(shutil.copy, operations)
    p.close()
    p.join()

if __name__ == "__main__":
    main()

2 Comments

Thank you for your help. I'll definitely test/use your solution, as I'll be handling large files most of the time, and I guess this will take advantage of more cores.
@alc, if was your favorite, mark it as useful and favorite too! ;)
1

In this example you define your file folder before, and an array of destination folders. Python then iterates through the destinations in a for loop. Note the use of os.path.join, which is a safe way of building file paths for cross-platform work.

import shutil
import os


full_file_path =  os.path.join('home', 'orig')
paths = [os.path.join('home', 'destA'), os.path.join('home', 'destB')]
for path in paths:
    shutil.copy(full_file_path, path)

1 Comment

Thank you. Very nice/simple solution. I'll test it soon and provide feedback. So far I just tested the one Mr.F suggested with loop wrapping. I'll come back if I need more help, but so far all the solutions here addressed what I needed. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.