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?