Search code examples
functioncsvpathzip

Function to zip a CSV file in python


I am trying to make a function which uses as arguments the source path, source file, target path, target file and that way convert an CSV file into zip. I am new to both python and this site so if you can please help me.


Solution

  • 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)