pythonduplicatescopynassolid-state-drive

copy files to another folder, avoiding duplication in python


I need your help. I'm a semi-beginner in python and I would like to create a program that allows me to copy the content (in this case photos) of a folder A to another folder B. However, the content of folder B may, potentially, already contain files from folder B.

So I need to avoid either copying them or deleting the duplicates afterwards. (The name is enough, there is no need to check the size etc.) I thought of lists which with the help of two nested "for" loops would detect identical names. I don't think this is the smartest solution.

How would you translate this into python?

Thanks a lot for your help.

I found that on the web: https://gist.github.com/vinovator/a2ba7306e829bf3a9010

PS: My final goal is to create a box with a rasbperry connected to a HDD and a SD card port that will allow me, as soon as I insert the Sd card in the "slot" to automatically save new pictures.


Solution

  • Hi get the path list of each folder and turn them into a set so you spot the difference between the two folders.

    import glob
    
    folderA = glob.glob("pathTofolderA")
    folderB = glob.glob("pathTofolderB")
    
    diffA = list(set(folderA) - set(folderB))
    diffB = list(set(folderB) - set(folderA))
    

    depending on the difference you are looking for your answer is diffA or diffB. More information on set here: https://realpython.com/python-sets/#operating-on-a-set