The shutil module in Python, part of the standard utility modules, provides a number of high-level operations on files and collections of files. This module comes in handy especially for tasks involving copying and archiving files and directory trees. Here’s an overview of some of the key functionalities offered by shutil:
1. Copying Files and Directories
shutil.copy(src, dst): Copies the filesrcto the file or directorydst. Ifdstspecifies a directory, the file will be copied intodstusing the base filename fromsrc.shutil.copy2(src, dst): Similar toshutil.copy, butcopy2also attempts to preserve all file metadata.shutil.copytree(src, dst): Recursively copies an entire directory tree rooted atsrcto a directory nameddstand attempts to preserve metadata. This function also offers the ability to ignore certain files or directories during the copy process with theignoreargument.
2. Moving Files and Directories
shutil.move(src, dst): Recursively moves a file or directory (src) to another location (dst). If the destination is on the current filesystem, thenos.rename()is used. Otherwise,srcis copied todstusingcopy2()and then removed.
3. Deleting Files and Directories
shutil.rmtree(path): Recursively deletes a directory tree;pathmust point to a directory (but not a symbolic link to a directory).
4. Disk Usage
shutil.disk_usage(path): Returns disk usage statistics about the given path as a named tuple with the attributestotal,used, andfree, which represent the total, used, and free disk space in bytes, respectively.
5. Archiving Operations
shutil.make_archive(base_name, format, root_dir): Create an archive file (such as zip or tar) and return its name.base_nameis the name of the file to create, including the path, minus any format-specific extension.formatis the archive format: one of “zip”, “tar”, “gztar”, “bztar”, or “xztar”.shutil.unpack_archive(filename, extract_dir, format): Unpacks an archive.filenameis the full path of the archive file,extract_diris the directory to unpack into, andformatis the archive format.
6. File Metadata
shutil.chown(path, user=None, group=None): Changes the owner and/or group of the given path to the numericuserandgroupids.shutil.which(cmd): Similar to the Unixwhichcommand, it returns the path to an executable which would be run if the givencmdwere called. If nocmdis found, returnsNone.
Example of Copying a File
Here’s a simple example of how to use shutil to copy a file:
import shutil
# Copy src file to dst file. The destination can be a directory.
shutil.copy('path/to/source/file.txt', 'path/to/destination/file.txt')
Example of Removing a Directory Tree
import shutil
# Recursively delete a directory tree
shutil.rmtree('path/to/directory')
The shutil module is very powerful for managing files and directories in Python, providing a higher-level interface that’s easier to use for complex operations compared to the os module that handles similar tasks at a lower level.