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
Syntax: shutil.move(source, destination, copy_function = copy2)
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 :
shutil.move() method to move file from source to destination
Before moving file: ['source'] After moving file: ['destination'] Destination path: C:/Users/Rajnish/Desktop/GeeksforGeeks/destination
Example #2 :
shutil.move() method to move file by using
shutil.copytree() method and the destination directory exists already.
Before moving file: ['destination', 'source'] After moving file: ['destination'] Destination path: C:/Users/Rajnish/Desktop/GeeksforGeeks/destination\source
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.