Python | shutil.move () method

Python Methods and Functions

The

Shutil module in Python provides many high-level functions for operations on files and collections of files. It is included in the standard Python utilities. This module helps automate the process of copying and deleting files and directories. 
shutil.move() 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 to that directory. If the destination already exists but is not a directory, it may be overwritten depending on os.rename () .

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 the shutil.move () method to move a file from source to destination

# Python program to explain the shutil.move () method

 
# import of the os module

import os 

 
# shutil module import

import shutil 

 
# path

  path = 'C: / Users / Rajnish / Desktop / GeeksforGeeks'

 
# List of files and directories
# in & # 39; C: / Users / Rajnish / Desktop / Python.Engineering & # 39;

print ( "Before moving file:"

print (os.listdir (path)) 

 

 
# Source path

source = 'C: / Users / Rajnish / Desktop / Python.Engineering / source'

 
# Destination path

destin ation = 'C: / Users / Rajnish / Desktop / Python.Engineering / destination'

 
# Move content
# source to destination

dest = shutil.move (source , destination) 

 
# List of files and directories
# in & quot; C: / Users / Rajnish / Desktop / Python.Engineering & quot;

print ( "After moving file:"

print (os.listdir (path)) 

 
# Print the path again
# generated file

print ( "Destination path:" , dest) 

Exit :

 Before moving file: ['source'] After moving file: [' destination'] Destination path: C: / Users / Rajnish / Desktop / Python.Engineering / destination 

Example # 2:
Using shutil.move () to move a file using the shutil.copytree () method and the target directory already exists.

# Python program to explain the shutil.move () method

 
# import of the os module

import os 

 
# shutil module import

import shutil 

 
# path

path = 'C: / Users / Rajnish / Desktop / GeeksforGeeks'

 
# List of files and directories
# in & # 39; C: / Users / Rajnish / Desktop / Python.Engineering & # 39;

print ( " Before moving file: "

print (os.listdir (path)) 

  

 
# Source path

source = 'C: / Users / Rajnish / Desktop / Python.Engineering / source'

  
# Destination path

destination = 'C: / Users / Rajnish / Desktop / Python.Engineering / destination'

  
# Move content
# source to destination
# using shutil.copytree () as parameter

dest = shutil.move (source, destination, copy_function = shutil.copytree) 

 
# List of files and directories
# in & quot; C: / User s / Rajnish / Desktop / Python.Engineering & quot;

print ( " After moving file: "

print (os.listdir (path)) 

 
# Print paths again
# generated file

print ( " Destination path: " , dest) 

Exit :

 Before moving file: ['destination',' source'] After moving file: ['destination'] Destination path: C: / Users / Rajnish / Desktop / Python.Engineering / destinationsource 




Tutorials