我正在python 2.7.12中编写一个函数来将文件夹及其所有内容压缩为zip文件以进行备份。我正在测试代码的文件包括一些指向我想要包含在备份文件中的不存在目录的链接。
到目前为止,我的代码如下:
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))
当它压缩链接时,我收到此错误:
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)'
其余的文件都得到了很好的备份,但无论出于什么原因我都无法破译,链接会破坏它。