Creating Two Zip Files with #example.txt#

  • Share this:

Code introduction


This function creates two zip files and adds a text file named 'example.txt' to both of them. The path of the first zip file is file1, and the second is file2.


Technology Stack : os, zipfile

Code Type : File operation

Code Difficulty : Intermediate


                
                    
def zip_file(file1, file2):
    import os
    import zipfile

    with zipfile.ZipFile(file1, 'w') as z1:
        z1.write('example.txt', arcname='example.txt')
    with zipfile.ZipFile(file2, 'w') as z2:
        z2.write('example.txt', arcname='example.txt')                
              
Tags: