I have a functionality in my Python which backups a particular directory everyday. The backup steps include compressing the directory. I am using shutil.make_archive
to compress the directory.
But now I am facing a permission issue when the directory I compress contains files which I do not have access to.
In this case, I just want to skip that file and then go ahead with compression of the remaining files. How do I achieve this ?
I searched SO and came across this answer, which shows how to zip a directory from using zipfile
library. In this case, I can just check if a particular file throws an exception and ignore it.
Is this the only way or is there an easier way ?
My code (just uses shutil.make_archive):
shutil.make_archive(filename, "zip", foldername)
The code in the answer which I have modified for my use:
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
try:
ziph.write(os.path.join(root, file))
except PermissionError as e:
continue
if __name__ == '__main__':
zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('tmp/', zipf)
zipf.close()
Please tell me if there is an easier/efficient solution to my issue.