File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ #! /usr/bin/python3.5
2+
3+ """
4+
5+ Author: Samrat Banerjee
6+ Dated: 29/06/2018
7+ Description: PROJECT- Copies an entire folder and its contents into a ZIP file whose filename increments
8+
9+ """
10+
11+ import zipfile , os
12+
13+ def backupToZip (folder ):
14+ # Backup the entire contents of "folder" into a ZIP file
15+
16+ folder = os .path .abspath (folder ) # make sure folder is absolute
17+
18+ # Figure out the filename this should use based on what files already exist
19+ number = 1
20+ while True :
21+ zipFilename = os .path .basename (folder )+ '_' + str (number )+ '.zip'
22+ if not os .path .exists (zipFilename ):
23+ break
24+ number = number + 1
25+
26+ # Create the ZIP file
27+ print ("Creating %s..." % (zipFilename ))
28+ backupZip = zipfile .ZipFile (zipFilename ,'w' )
29+
30+ # Walk the entire folder tree and compress the files in each folder
31+ for foldername ,subfolders ,filenames in os .walk (folder ):
32+ print ("Adding files in %s..." % (foldername ))
33+ # Add the current folder to the ZIP file
34+ backupZip .write (foldername )
35+ # Add all the files in this folder to the ZIP file
36+ for filename in filenames :
37+ newBase = os .path .basename (folder )+ '_'
38+ if filename .startswith (newBase ) and filename .endswith ('.zip' ):
39+ continue # don't backup the backup ZIP files
40+ backupZip .write (os .path .join (foldername , filename ))
41+ backupZip .close ()
42+ print ("Done." )
43+
44+ backupToZip ('/home/samrat/LearningAutomationwithPython' )
You can’t perform that action at this time.
0 commit comments