Creating a Zip Archive of a Directory in Python
When working with file systems, creating an archive of a directory structure can be a useful task. Python offers convenient methods to accomplish this, particularly through the shutil.make_archive function.
Using shutil.make_archive
To create a zip archive of a directory using shutil.make_archive, follow these steps:
Call shutil.make_archive(output_filename, 'zip', dir_name):
Example:
import shutil shutil.make_archive('my_archive', 'zip', 'my_directory')
This code will create a zip archive named 'my_archive.zip' containing the contents of the 'my_directory' folder.
Advanced Options
The shutil.make_archive function provides additional parameters if you need more customization. However, for creating basic zip archives, the method described above will suffice. If you require more fine-grained control over the archiving process, you may want to explore the zipfile module for further customization.
The above is the detailed content of How Can I Create a Zip Archive from a Directory in Python?. For more information, please follow other related articles on the PHP Chinese website!