On Jul 16, 1:42 pm, [EMAIL PROTECTED] wrote: > This script uses a simple for loop to zip some files. However I am > repeating code that cries out for a nested loop. My two lists of > files_to_be_zipped (spare and seekfacts) are of uneven length so I > can't seem to decipher the "for_logic". I would appreciate any help. > Thanks, Bill > > import zipfile > import os > > zips = [ > 'c:/spare.zip', > 'c:/seekfacts.zip' > ] > spare = [ > 'c:/spare/huge.fm3', > 'c:/spare/huge.wk3' > ] > seekfacts = [ > 'c:/seekfacts/bookmark.html', > 'c:/seekfacts/index.htm', > 'c:/seekfacts/seek.css', > 'c:/seekfacts/seek.js' > ] > > zFile = zipfile.ZipFile(zips[0], 'w') > for files in spare: > zFile.write(files, os.path.basename(files), zipfile.ZIP_DEFLATED) > zFile.close() > > zFile = zipfile.ZipFile(zips[1], 'w') > for files in seekfacts: > zFile.write(files, os.path.basename(files), zipfile.ZIP_DEFLATED) > zFile.close()
I would do something like this: # UNTESTED import zipfile import os zip_dict = { 'spare' : ['c:/spare.zip', 'c:/seekfacts.zip'], 'seekfacts' : [ 'c:/seekfacts/bookmark.html', 'c:/seekfacts/index.htm', 'c:/seekfacts/seek.css', 'c:/seekfacts/seek.js' ] } for key,value in zip_dict.items(): zFile = zipfile.ZipFile("c:/%s.zip" % key, 'w') for fname in value: zFile.write(fname, os.path.basename(files), zipfile.ZIP_DEFLATED) zFile.close() # End untested code. This implicitly maps thing with the key foo to the zip file c:/ foo.zip, but if you want to be more general, I would suggest thinking about making a class. -Dan -- http://mail.python.org/mailman/listinfo/python-list