Create and Extract Zip File with Specified Files

  • Share this:

Code introduction


This function creates a zip file, adds specified files to it, and then extracts the zip file to a specified directory.


Technology Stack : zipfile

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zipfile_create_extract(path_to_zip, *file_paths):
    import zipfile
    with zipfile.ZipFile(path_to_zip, 'w') as zipf:
        for file_path in file_paths:
            zipf.write(file_path, arcname=file_path.split('/')[-1])
    with zipfile.ZipFile(path_to_zip, 'r') as zipf:
        zipf.extractall(path_to_zip + "_extracted")                
              
Tags: