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)
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. P >
Example # 1:
shutil.move () method to move a file from source to destination p>
Before moving file: ['source'] After moving file: [' destination'] Destination path: C: / Users / Rajnish / Desktop / Python.Engineering / destination
Example # 2:
shutil.move () to move a file using the
shutil.copytree () method and the target directory already exists.
| tr> |
Before moving file: ['destination',' source'] After moving file: ['destination'] Destination path: C: / Users / Rajnish / Desktop / Python.Engineering / destinationsource