file_tools.py 661 Bytes
import os
from zipfile import ZipFile


def file_write(file, file_path):
    with open(file_path, 'wb+') as f:
        for chunk in file.chunks():
            f.write(chunk)


def write_zip_file(dir_name, zipfile_path):
    if not os.path.isdir(dir_name):
        return
    with ZipFile(zipfile_path, 'w') as z:
        for root, dirs, files in os.walk(dir_name):
            root_target_path = root.replace(dir_name, '')
            for single_file in files:
                src_file_path = os.path.join(root, single_file)
                file_target_path = os.path.join(root_target_path, single_file)
                z.write(src_file_path, file_target_path)