Python – Move and overwrite files and folders

In this article, we will be learning on moving a collection of files and folders where there may be files/folders with the same name as in the source name in the destination. So that we may need to overwrite the existing destination file with the source file.

The shutil.move() method is used to move a file or directory from one place to another. If there is an existing directory or file in the destination which will be checked using os.path.isfile() and os.path.isdir() method, then it will be deleted using os.remove() method, and if it is a directory then it will be deleted using shutil.rmtree() method then the file will be moved.

Syntax:

shutil.move(source, destination, copy_function = copy2)

Parameters:

  • source: A string representing the path of the source file.
  • destination: A string representing the path of the destination directory.
  • copy_function (optional): The default value of this parameter is copy2. We can use other copy function like copy, copytree, etc for this parameter.

Return Value: This method returns a string which represents the path of newly created file.

Call shutil.move(source, destination) method by replacing source and destination by entire path in string format. Using the above method, the files with the same name will be overwritten with the file content as of the source file

Example 1: Program to move a folder containing a file using python.

Folder Hierarchy:

Desktop
     |_folder_
              |_Beginner folder
              |_test folder_gfg.txt

Python3




# importing os module
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 = 'Beginner 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)


Output:

Example 2: Program to overwrite a folder containing a file using python.

Folder Hierarchy:

Desktop
     |_folder_
              |_Beginner folder_test folder_gfg.txt
              |_test folder_gfg.txt

Python3




# importing os module
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 = 'Beginner 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)


Output: