The program should backup files in a source folder to a zip file in destination, and keep track of how many versions there are (which explains the shelve). In order to copy these files, apparently I have to change the
cwd to the source path (folder), because otherwise the program will not detect them. While it may not seem to cause that much of an issue, my OCD triggers when I see the shelve files in the new
.zip folder. I thought about adding lines to delete the shelve files, but I was wondering if there is some other way.
(This program is a project in a certain book I'm reading, and while the project demanded a more basic version of this, I like to take it a step forward.)
import os, shelve, zipfile def backupToZip(folder, destination, copies=1): #Folder fName= os.path.basename(folder) #Shelfing for copy number namesNums = shelve.open('names') if fName not in namesNums: num= 1 namesNums[fName]= num else: num= namesNums[fName] #Producing zips os.chdir(folder) for i in range(int(copies)): zipPath= os.path.join(destination, fName+'_'+str(num)+'.zip') theZip= zipfile.ZipFile(zipPath, 'w') for foldername, subfolders, filenames in os.walk(folder): for filename in filenames: theZip.write(filename) num+=1 namesNums[fName]= num namesNums.close()