0

I have a folder (C:\\Python27\\security_camera_snapshot\\) where image1.png 2.png 3.png .... 100.png is stored. I need to make them in one.zip file and upload it to a NAS server.

But how can i do the compression of all those files in zip?

  • With the [zipfile module](https://docs.python.org/2/library/zipfile.html)? – Aran-Fey Jul 24 '16 at 09:11
  • would this help? http://stackoverflow.com/questions/296499/how-do-i-zip-the-contents-of-a-folder-using-python-version-2-5 – Yu Zhang Jul 24 '16 at 09:11
  • 2
    Possible duplicate of [How to create a zip archive of a directory](http://stackoverflow.com/questions/1855095/how-to-create-a-zip-archive-of-a-directory) – Isdj Jul 24 '16 at 09:11

1 Answers1

1

Just use the zipfile library as in this example adapted for your usage :

#!/usr/bin/env python
import os
import zipfile

def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file))

if __name__ == '__main__':
    zipf = zipfile.ZipFile('One.zip', 'w', zipfile.ZIP_DEFLATED)
    zipdir('C:\\Python27\\security_camera_snapshot\\', zipf)
    zipf.close()
Germain
  • 658
  • 7
  • 19