0

I have several directories with filenames being the same, but their data inside is different. My program identifies these files (among many others) and I would like to copy all the matches to the same directory.

I am using shutil.copy(src,dst) but I don't want to overwrite files that already exist in that directory (previous matches) if they have the same name. I'd like to be able to append an integer if it already exists. Similar to the behavior in Windows10 when you copy where you can "keep both versions".

So for example, if I have file.txt in several places, the first time it would copy into dst directory it would be file.txt, the next time it would be file-1.txt (or something similar), and the next time it would be file-2.txt.

Are there any flags for shutil.copy or some other copy mechanism in Python that I could use to accomplish this?

bmikolaj
  • 485
  • 1
  • 5
  • 16
  • 1
    Duplicated. Please refer to this: https://stackoverflow.com/questions/18383384/python-copy-files-to-a-new-directory-and-rename-if-file-name-already-exists or this: https://stackoverflow.com/questions/28960274/move-file-to-a-folder-or-make-a-renamed-copy-if-it-exists-in-the-destination-fol – Nolan Edric Jul 21 '21 at 15:20
  • 1
    This is also kinda related: https://stackoverflow.com/questions/33282647/python-shutil-copy-if-i-have-a-duplicate-file-will-it-copy-to-new-location – Nolan Edric Jul 21 '21 at 15:28

0 Answers0