Search code examples
pythoncopy

Python copying multiple files simultaneously (Multithreading)


I have some python code that I use to copy files from one directly to another, however it is a very slow process and is sequential. I would like to find a way to do multithreading, copying more that one by one file, but rather have 10 or 100 processes running doing this. Here is my current core that process one by one file:

import os
import shutil

directory = "All_files"

with open('files.txt','w') as f:
    for filename in os.listdir(directory):
        src = directory+"/"+filename+"/02-Partners.pdf"
        file_exists = os.path.exists(src)
        if file_exists == True:
            dst="part/"+filename+"_"+"02-Partners.pdf"
            shutil.copyfile(src,dst)
            f.write('%s \n' % (filename))

Any assistance in getting multiple files copying at the same time will be appreciated.


Solution

  • Take a look at python's threading basic package and see if it fits you.

    https://docs.python.org/3/library/threading.html