Python Forum
Shutil move if file exists in destination - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Shutil move if file exists in destination (/thread-24158.html)



Shutil move if file exists in destination - Friend - Feb-02-2020

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 ?


RE: Shutil move if file exists in destination - klllmmm - Feb-02-2020

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')



RE: Shutil move if file exists in destination - Friend - Feb-02-2020

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