Python's standard library includes a versatile module called "shutil" that provides a set of high-level file operations. The shutil module is designed to simplify the task of working with files and directories, making it easier for Python developers to perform common file and directory operations without having to write low-level code.

The shutil module provides several functions that can be used to copy, move, and delete files and directories. These functions are platform-independent and work across different operating systems, making it easy to write portable Python scripts that can run on different platforms. In this article, we'll explore some of the most commonly used functions in the shutil module and provide examples to illustrate their usage.

Copying files

The shutil module provides the copy function that can be used to copy files from one location to another. The basic implementation for the copy function is as follows:

import shutil

src = "example.txt"
dest = "backup/example.txt"
shutil.copy(src, dest)

Here, src is the source file that you want to copy, and dst is the destination location where you want to copy the file. The above example shows how to use the copy function to copy a file named example.txt from the current directory to a directory named backup.

Moving files

The move function in the shutil module can be used to move files from one location to another. The basic usage for the move function is as follows:

import shutil

src = "example.txt"
dest = "archive/example.txt"
shutil.move(src, dest)

Here, src is the source file that you want to move, and dst is the destination location where you want to move the file. The above example shows how to use the move function to move a file named example.txt from the current directory to a directory named archive.

Deleting files

The remove function in the shutil module can be used to delete files. The basic example for the remove function is as follows:

import shutil

shutil.remove("example.txt")

The above example shows how to use the remove function to delete a file named example.txt from the current directory.

Copying directories

The copytree function in the shutil module can be used to copy directories and their contents.

import shutil

shutil.copytree("source", "destination")

The following example shows how to use the copytree function to copy a directory named source to a directory named destination.

Moving directories

The "move" function in the shutil module can also be used to move directories and their contents.

import shutil

shutil.move("source", "destination")

The following example shows how to use the move function to move a directory named source to a directory named destination.

Deleting directories

The rmtree function in the shutil module can be used to delete directories and their contents.

import shutil

shutil.rmtree("source")

The following example shows how to use the rmtree function to delete a directory named source.

Archiving files and directories

The shutil module provides functions for creating and extracting archive files. The make_archive function can be used to create an archive file, while the unpack_archive function can be used to extract an archive file.

import shutil

base_name = "archive"
format = "zip"
root_dir = "source"
shutil.make_archive(base_name, format, root_dir)

Here, base_name is the name of the archive file that you want to create, format is the format of the archive file (e.g., zip, tar, gztar, bztar, xztar), and root_dir is the directory that you want to archive.

The basic syntax for the unpack_archive function is as follows:

shutil.unpack_archive("archive.zip", "destination")

The above example shows how to use the unpack_archive function to extract the files from a ZIP archive named archive.zip to a directory named destination.

Conclusion

The shutil module in Python is a powerful tool that can help you perform a variety of file and directory operations in a simple and efficient manner. With its high-level functions for copying, moving, deleting, and archiving files and directories, the shutil module can save you a lot of time and effort when working with files in Python. By mastering the shutil module, you can become a more efficient Python developer and be better equipped to handle a wide range of file-related tasks.

For more details about this module in python, visit official documentation.

https://docs.python.org/3/library/shutil.html