Python - Move and overwrite files and folders - GeeksforGeeks

31 Aug, 2021
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_
              |_Geeks folder
              |_test folder_gfg.txt
Python3
# importing os moduleimport os # importing shutil moduleimport shutil # pathpath = 'C:/Users/KIIT/Desktop/folder' # List files and directoriesprint("Before moving file:")print(os.listdir(path))  # Assign source and destinationsource = 'test folder'destination = 'Geeks folder' sourcePath = path+'/'+sourcedestinationPath = path+'/'+destination # Check if file already existsif 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 destinationdest = shutil.move(sourcePath, destinationPath) # List files and directoriesprint("After moving file:")print(os.listdir(path)) print(source, 'has been moved!') # Print new path of fileprint("Destination path:", dest)
Output:
Example 2: Program to overwrite a folder containing a file using python.
Folder Hierarchy:
Desktop
     |_folder_
              |_Geeks folder_test folder_gfg.txt
              |_test folder_gfg.txt
Python3
# importing os moduleimport os # importing shutil moduleimport shutil # pathpath = 'C:/Users/KIIT/Desktop/folder' # List files and directoriesprint("Before moving file:")print(os.listdir(path))  # Assign source and destinationsource = 'test folder'destination = 'Geeks folder' sourcePath=path+'/'+sourcedestinationPath=path+'/'+destination # Check if file already existsif 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 destinationdest = shutil.move(sourcePath, destinationPath) # List files and directoriesprint("After moving file:")print(os.listdir(path)) print(source,'has been moved!') # Print new path of fileprint("Destination path:", dest)
Output:
gulshankumarar231
Picked
Python directory-program
python-os-module
Python-shutil
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()