python create zip file from directory code example
Example 1: python zip folder
import shutil
shutil.make_archive(output_filename, 'zip', dir_name)
Example 2: python zip folder
import os
import zipfile
def zipdir(path, ziph):
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file))
if __name__ == '__main__':
zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('tmp/', zipf)
zipf.close()
Example 3: 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)