You can download this code by clicking the button below.
This code is now available for download.
This function creates a ZIP file containing random contents. It first defines an internal function `make_file` to create files with random content, then uses the `zipfile` module to create the ZIP file and add these files, and finally returns the path of the ZIP file.
Technology Stack : zipfile, random
Code Type : Function
Code Difficulty : Intermediate
import math
import random
import os
import shutil
import zipfile
import json
import datetime
import re
import socket
import sys
def generate_random_zip_file(zip_path, contents):
def make_file(filename):
with open(filename, 'w') as f:
f.write(random.choice(contents))
with zipfile.ZipFile(zip_path, 'w') as zipf:
for i in range(5):
filename = f'file_{i}.txt'
make_file(filename)
zipf.write(filename, arcname=filename)
os.remove(filename)
return zip_path