首页 > 解决方案 > 在 python 中压缩 CSV 文件的函数

问题描述

我正在尝试创建一个函数,它使用源路径、源文件、目标路径、目标文件作为参数,然后将 CSV 文件转换为 zip。我对 python 和这个网站都是新手,所以如果你能帮助我。

标签: functioncsvpathzip

解决方案


import os
from zipfile import ZipFile

def create_zip(source_path, source_file, target_path, target_file):
    # get full path of source and target
    source_filepath = os.path.join(source_path, source_file)
    target_filepath = os.path.join(target_path, target_file)
    # make a new empty zip file at target
    with ZipFile(target_filepath, 'w') as new_zipfile:
        # write source into it
        new_zipfile.write(source_filepath)

推荐阅读