'shutil - check if file exists rename and paste
I am using shutil to copy files from one location to the other. I have a process where these files are generated periodically from an independent system with the same file name.
I have to move these files to a folder dump which have a file with the same name. I usually have to append the file name and then drop it to avoid conflict.
I was wondering if I can do it programmatically.
I am using this code
import shutil
print('Selection File')
shutil.copy2(r"H:\My Drive\Selection Build.xlsx",
r"C:\Users\User\Documents\Selection Build.xlsx")
But it overwrites the files while I want to check if the file exists then append the file and then copy it.
e.g. if Selection Build.xlsx exists in \User\Documents\ then rename the file to Selection Build(1).xlsx or if Selection Build(n).xlsx exists then rename it to Selection Build(n+1).xlsx and then copy the file.
How can I do it?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
