Hi Team,
I am trying to move folders and subfolders. from one location to another.
I want to override folders.
getting error :
raise Error("Destination path '%s' already exists" % real_dst)
shutil.Error: Destination path 'E:\DATA\d\A' already exists
import shutil
# Source path
source = "E:\DATA\A"
# Destination path
destination = "E:\DATA\d"
# Move the content of
# source to destination
dest = shutil.move(source, destination)
I actually tried your code and it worked (linux).
I have to change the path to a linux path, I wouldn't expect that to be an issue.
Hi Larzoo,
If I try to send second time,
I get error folder already exists.
My intention is I want to take back up in some other folder,
I am ok we add date to destination folder and save the files.
Thanks
mg
Hi Larz60,
thanks for your help.
sometimes user may delete folder mistakenly, code will not work.
directory will not exists. it will throw error.
its only backup, clearing source folder and creating directory dynamically and putting files.
I am thinking of creating destinationfolder with date_time, always it will be unique, and we can pass there.
import os
# importing shutil module
import shutil
# path
path = 'C:/Users/KIIT/Desktop/folder'
# List files and directories
print("Before moving file:")
print(os.listdir(path))
# Assign source and destination
source = 'test folder'
destination = 'Geeks folder'
sourcePath = path+'/'+source
destinationPath = path+'/'+destination
# Check if file already exists
if os.path.isdir(destinationPath+'/'+source):
print(source, 'exists in the destination path!')
shutil.rmtree(destinationPath+'/'+source)
elif os.path.isfile(destinationPath+'/'+source):
os.remove(destinationPath+'/'+source)
print(source, 'deleted in', destination)
# Move the content
# source to destination
dest = shutil.move(sourcePath, destinationPath)
# List files and directories
print("After moving file:")
print(os.listdir(path))
print(source, 'has been moved!')
# Print new path of file
print("Destination path:", dest)
timestamps are indeed a better idea, you can create code to purge the backup as required.