Open In App

Python | shutil.move() method

Improve
Improve
Like Article
Like
Save
Share
Report

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) 


Output:

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 parameter
dest = 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) 


Output:

Before moving file:
['destination', 'source']
After moving file:
['destination']
Destination path: C:/Users/Rajnish/Desktop/GeeksforGeeks/destination\source


Last Updated : 20 Jun, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads