Python Forum
python move folders and subfolders not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python move folders and subfolders not working
#1
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)
Reply
#2
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.
Reply
#3
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
Reply
#4
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-mov...d-folders/
Reply
#5
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)
Reply
#6
timestamps are indeed a better idea, you can create code to purge the backup as required.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  mouse move event/cinfiguration ttk/python janeik 4 1,077 Jul-03-2023, 05:30 PM
Last Post: deanhystad
  python move specific files from source to destination including duplicates mg24 3 1,103 Jan-21-2023, 04:21 AM
Last Post: deanhystad
  Copy files from subfolders into same name of subfolders at other directory rathoreanil 1 2,354 Oct-12-2020, 01:30 AM
Last Post: Larz60+
  sub-folders in folders from text line jenost 1 1,579 Mar-31-2020, 07:16 AM
Last Post: ndc85430
  Python script that recursively zips folders WITHOUT nesting the folder inside the zip umkc1 1 2,858 Feb-11-2020, 09:12 PM
Last Post: michael1789
  how difficult its to move to Python 3.8 from Python 2.7 dkirandasari 4 3,207 Jan-23-2020, 07:42 PM
Last Post: DeaD_EyE
  How to copy files from subfolders into one folder silfer 3 8,416 Aug-29-2019, 08:36 AM
Last Post: DeaD_EyE
  Python Script to repeat Photoshop action in folders and subfolders silfer 2 4,568 Jul-25-2019, 03:12 PM
Last Post: silfer
  How can I move in an external program via Python? Pythoner 0 2,280 Dec-29-2018, 12:43 PM
Last Post: Pythoner
  python zipfile.ZipFile makes zip file but can't add folders to them CashMunny 3 3,653 May-09-2018, 10:28 PM
Last Post: CashMunny

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020