shutil.move if directory already exists
In addition to the code above I move folder into already existing directories and this collision will produce an error so I recommend shutil.copytree()
shutil.copytree('path_to/start/folder', 'path_to/destination/folder', dirs_exist_ok=True)
The dirs_exist_ok=True
is required to allow overwriting files, otherwise you will get an error.
Use copy insted of move, it should overwrite files automatically
shutil.copy(sourcePath, destinationPath)
Then of course you need to delete original files. Be aware, shutil.copy
does not copy or create directories, so you need to make sure they exist.
If this does not work either, you can manually check if file exists, remove it, and move new file:
To check that file exists, use:
from pathlib import Path
my_file = Path("/path/to/file")
if my_file.exists():
to check that something at path exist
if my_file.is_dir():
to check if directory exists
if my_file.is_file():
to check if file exists
To delete directory with all its contents use:
shutil.rmtree(path)
Or delete a single file with
os.remove(path)
and then move them one by one