python zip a file zipfile code example
Example 1: make zipfile from directory py
import zipfile
filePaths = []
for root, directories, files in os.walk("MyDirectoryPath"):
for filename in files:
filePath = os.path.join(root, filename)
filePaths.append(filePath)
z = zipfile.ZipFile("MyDirectoryPathWithZipExt.zip", 'w')
with z:
for file in filePaths:
z.write(file)
Example 2: python zip function
>>> letters = ['a', 'b', 'c']
>>> numbers = [0, 1, 2]
>>> for l, n in zip(letters, numbers):
... print(f'Letter: {l}')
... print(f'Number: {n}')
...
Letter: a
Number: 0
Letter: b
Number: 1
Letter: c
Number: 2