Python Forum

Full Version: Shutil move if file exists in destination
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

i am just learning about the modul shutil and tried to move a file to a destination folder.
It works. However if i try to run the script again and overwrite the file, it doesnt work, because the file exists alreay in the destination.

shutil.move(r'C:\Users\user\Desktop\Source\t.txt', r'C:\Users\user\Desktop\Destination')
is there a way to overwrite the file if it exits ?
You may have to remove the file if it exists already

if os.path.exists(r'C:\Users\user\Desktop\Source\t.txt'):
   os.remove(r'C:\Users\user\Desktop\Source\t.txt')
i guess it should be like this (destination instead of source):
if os.path.exists(r'C:\Users\user\Desktop\Destination\t.txt'):
   os.remove(r'C:\Users\user\Desktop\Destination\t.txt')
hmm i thought there is some other way, without having to do this "manually"

thanks a lot