Python | shutil.move() method

20 Jun, 2019
Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating the process of copying and removal of files and directories.shutil.move() method Recursively moves a file or directory (source) to another location (destination) and returns the destination. If the destination directory already exists then src is moved inside that directory. If the destination already exists but is not a directory then it may be overwritten depending on os.rename() semantics.
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.
Example #1 :
Using shutil.move() method to move file from source to destination
# Python program to explain shutil.move() method       # importing os module import os   # importing shutil module import shutil   # path path = 'C:/Users/Rajnish/Desktop/GeeksforGeeks'  # List files and directories # in 'C:/Users/Rajnish/Desktop/GeeksforGeeks' print("Before moving file:") print(os.listdir(path))     # Source path source = 'C:/Users/Rajnish/Desktop/GeeksforGeeks/source'  # Destination path destination = 'C:/Users/Rajnish/Desktop/GeeksforGeeks/destination'  # Move the content of # source to destination dest = shutil.move(source, destination)   # List files and directories # in "C:/Users / Rajnish / Desktop / GeeksforGeeks" print("After moving file:") print(os.listdir(path))   # Print path of newly # created file print("Destination path:", dest) 
Before moving file:
['source']
After moving file:
['destination']
Destination path: C:/Users/Rajnish/Desktop/GeeksforGeeks/destination

Example #2 :Using shutil.move() method to move file by using shutil.copytree() method and the destination directory exists already.
# Python program to explain shutil.move() method       # importing os module import os   # importing shutil module import shutil   # path path = 'C:/Users/Rajnish/Desktop/GeeksforGeeks'  # List files and directories # in 'C:/Users/Rajnish/Desktop/GeeksforGeeks' print("Before moving file:") print(os.listdir(path))     # Source path source = 'C:/Users/Rajnish/Desktop/GeeksforGeeks/source'  # Destination path destination = 'C:/Users/Rajnish/Desktop/GeeksforGeeks/destination'  # Move the content of # source to destination# using shutil.copytree() as parameterdest = shutil.move(source, destination, copy_function = shutil.copytree)   # List files and directories # in "C:/Users / Rajnish / Desktop / GeeksforGeeks" print("After moving file:") print(os.listdir(path))   # Print path of newly # created file print("Destination path:", dest) 
Before moving file:
['destination', 'source']
After moving file:
['destination']
Destination path: C:/Users/Rajnish/Desktop/GeeksforGeeks/destination\source

Python-shutil
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts