Zip and Rename File to Original Name

  • Share this:

Code introduction


Pack a file into a zip format and then rename the zip file to the original file name.


Technology Stack : os, zipfile, shutil

Code Type : File operation

Code Difficulty : Intermediate


                
                    
def zipfiles(file1, file2):
    import os
    import zipfile
    import shutil

    with zipfile.ZipFile(file1, 'w') as zipf:
        zipf.write(file2, os.path.basename(file2))

    shutil.move(file1, file2)                
              
Tags: