Open In App

Python | shutil.move() method

Last Updated : 28 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
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 removing files and directories.

The shutil.move() method moves a file or directory from a source to a destination. It handles directories recursively, moving the source into the destination directory if it exists. If the destination already exists as a file, it may be overwritten, depending on the behavior of os.rename(). The method returns the new path of the destination.

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 functions like copy, copytree, etc for this parameter.

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

Example 1: Using shutil.move()files method to move a file from source to destination.

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

Next Article
Article Tags :
Practice Tags :

Similar Reads