1

I'm writing a function which takes a set of files then either:

  • saves the files to a zip file if they are present.
  • or extract files from said zip file if they are not present.

Saving the files to a zip is working fine, but when it comes to extracting the files from my zip, the zip is emptied but no files are saved to the path that I have pointed extractall to. No errors are raised in the console.

Does anyone know what I can do to fix this? I've attempted manually closing the zip file after the except clause in case that is causing the first with to not unload properly, but that didn't appear to fix anything.

Code:

from zipfile import ZipFile

files = ['Combine1.docm', 'Combine2.docm', 'Combine3.docm',
        'Combine4.docm', 'Combine5.docm', 'FinalMaster.docm',
        'FinalMasterBeltDrier.docm', 'FinalMasterExport.docm']

backupZip = 'WorkaroundBackup.zip'

def ZipSave(files, backupZip):             
    #backs up files to zip if present, restores from backup zip if not present
    try:
        with ZipFile(backupZip, 'w') as zipWrite:
            for file in files:
                zipWrite.write(file, compress_type=None)
        
        print('Files backed up to %s.' %(backupZip))
        return

    except FileNotFoundError:   #catches files not present case
        with ZipFile(backupZip, 'r') as zipRead:
            zipRead.extractall(path = 'F:\DW Master\Template Docs Workaround')
        
        print('Files not found; restoring from %s.' %(backupZip))
        return
            
ZipSave(files, backupZip)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
MaccyF
  • 11
  • 1

0 Answers0