Python | shutil.move() method

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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) 

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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) 

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.