2

I am writing a function in python 2.7.12 to compress a folder and all its contents into a zip file for backup purposes. The files I am testing the code on include some links to non-existent directories that I would like to include in the backup file.

So far, my code is as follows:

def backupToZip(folder, zipPath):
    folder = os.path.abspath(folder)
    with zipfile.ZipFile(zipPath, 'a', zipfile.ZIP_DEFLATED) as backupZip:
        for foldername, subfolders, filenames in os.walk(folder):
            print('Adding files in %s...' % (foldername))
            backupZip.write(foldername)
            for filename in filenames:
                backupZip.write(os.path.join(foldername, filename))

When it gets to compressing the link however, I get this error:

    backupZip.write(os.path.join(foldername, filename))
  File "/usr/lib/python2.7/zipfile.py", line 1123, in write
    st = os.stat(filename)
OSError: [Errno 2] No such file or directory: '(path to the broken link)'

The rest of the files are getting backed up just fine, but for whatever reason that I can't decipher, the link breaks it.

Noah S.
  • 46
  • 5

0 Answers0