Python: create zipfile
To create a zip file in Python, you can use the zipfile
module. Here's an example of how to create a zip file:
import zipfile
# Create a new zip file
with zipfile.ZipFile('my_archive.zip', 'w') as zipf:
# Add files to the zip file
zipf.write('file1.txt')
zipf.write('file2.txt')
zipf.write('file3.txt')
In this example, we import the zipfile
module and use the ZipFile
class to create a new zip file called "my_archive.zip". We then use the write
method to add files to the zip file. In this case, we add three files: "file1.txt", "file2.txt", and "file3.txt".
You can replace the file names with the actual names of the files you want to add to the zip file. Make sure the files are in the same directory as your Python script, or provide the full path to the files if they are located elsewhere.
That's it! The code will create a zip file with the specified files.