What is shutil module?
This module offers a number of high-level operations on files and collection of files. Using it, we can move, copy and rename our files. It has many useful functions such as copyfile(), copymode(), etc. In this article, we will use the move() method of the shutil module to move our files from one place to another.
Note: os and shutil modules are come under Python standard library so, we do not have to install them by pip command we are just importing them in our program.
Syntax of shutil.move()
It recursively moves a file or directory from one place to another, then returns the final destination. Src is placed into the destination directory if it already exists. Depending on the semantics of os.rename(), a destination that already exists but is not a directory may be overwritten.
Syntax: shutil.move(source, destination, copy_function = copy2)
Parameters:
source: path of the source destination directory
destination: 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.
Automate Renaming and Organizing Files with Python
In this article, we are going to know how to automate renaming and organizing files with Python, hence, the article is divided into two sections: one teaches us how to organize files and the latter how to rename files.