python move folders and subfolders not working - 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: python move folders and subfolders not working (/thread-38636.html) |
python move folders and subfolders not working - mg24 - Nov-08-2022 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) RE: python move folders and subfolders not working - Larz60+ - Nov-08-2022 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. RE: python move folders and subfolders not working - mg24 - Nov-08-2022 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 RE: python move folders and subfolders not working - Larz60+ - Nov-08-2022 Windows may require that the destination directory not exist before a move. Linux will overwrite. if a backup, you should be copying, not moving, correct? This should proove helpful: https://www.geeksforgeeks.org/python-move-and-overwrite-files-and-folders/ RE: python move folders and subfolders not working - mg24 - Nov-09-2022 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) RE: python move folders and subfolders not working - Larz60+ - Nov-09-2022 timestamps are indeed a better idea, you can create code to purge the backup as required. |