Reputation: 309
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!
Upvotes: 1
Views: 3861
Reputation: 634
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)
Upvotes: 1
Reputation: 859
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()
Upvotes: 1
Reputation: 936
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)
Upvotes: 1