1. Introduction
Shutil is a Python module that provides several high-level operations for working with files and collections of files. It offers functionalities such as copying, moving, archiving, and compressing files. This article will focus specifically on the capabilities of shutil for copying and compressing files.
2. Copying Files
The shutil module provides a simple way to copy files and directories. The shutil.copy()
function can be used to copy a single file from a source location to a destination. The code snippet below demonstrates how to copy a file:
import shutil
src_file = 'path/to/source/file.txt'
dest_file = 'path/to/destination/file.txt'
shutil.copy(src_file, dest_file)
The shutil.copy()
function preserves the file's attributes, such as its timestamps and permissions. If the destination file already exists, it will be overwritten by the source file.
2.1 Copying Directories
In addition to copying individual files, shutil also provides an option to copy entire directories. The shutil.copytree()
function can be used to recursively copy a directory and its contents. Here's an example:
import shutil
src_dir = 'path/to/source/directory'
dest_dir = 'path/to/destination/directory'
shutil.copytree(src_dir, dest_dir)
The copytree()
function creates the destination directory if it doesn't exist and copies all the files and subdirectories from the source directory to the destination directory.
Note: When copying directories, it's important to ensure that the destination directory doesn't already exist. Otherwise, an error will be raised.
3. Compressing Files
Shutil also provides methods for compressing files and directories. The shutil.make_archive()
function can be used to create different types of archive files, including zip, tar, and gztar. Here's an example of creating a zip archive:
import shutil
directory_to_compress = 'path/to/directory'
shutil.make_archive('compressed_archive', 'zip', directory_to_compress)
This will create a zip archive named "compressed_archive.zip" containing all the files and directories from the specified directory.
3.1 Adjusting Compression Level
The shutil.make_archive()
function allows you to adjust the compression level of the archive. The default compression level is 6. You can modify the compression level by passing the format
parameter with the desired level, ranging from 0 to 9. A higher level means better compression but longer processing time. Here's an example:
import shutil
directory_to_compress = 'path/to/directory'
shutil.make_archive('compressed_archive', 'zip', directory_to_compress, dry_run=True, temperature=0.6)
In the example above, the compression level is adjusted to 0.6, which is lower than the default level. This can be useful if you want to prioritize speed over compression efficiency.
3.2 Extracting Archives
The shutil.unpack_archive()
function can be used to extract files from an archive. It supports extracting zip, tar, and gztar archives. Here's an example:
import shutil
archive_to_extract = 'path/to/archive.zip'
extract_location = 'path/to/extract/location'
shutil.unpack_archive(archive_to_extract, extract_location)
This will extract all the files and directories from the archive into the specified extract location.
4. Conclusion
The shutil module in Python provides a convenient way to perform file-related operations such as copying and compressing files. This article covered the basics of using shutil for copying files and directories and demonstrated how to compress and extract archives. With the help of shutil, managing file operations becomes more efficient and streamlined in Python.