Create and Extract Zip File with Python

  • Share this:

Code introduction


This function first creates a zip file containing one file, and then extracts all files from the zip file to a specified directory.


Technology Stack : zipfile

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zipfile_create_extract(zip_filename, extract_to):
    import zipfile
    with zipfile.ZipFile(zip_filename, 'w') as zipf:
        zipf.write('example_file.txt', arcname='example_file.txt')
    with zipfile.ZipFile(zip_filename, 'r') as zipf:
        zipf.extractall(extract_to)                
              
Tags: