For_loops hurt my brain.

Fredrik Lundh fredrik at pythonware.com
Wed Jul 16 16:24:22 EDT 2008


bsagert at gmail.com wrote:

> This script uses a simple for loop to zip some files. However I am
> repeating code that cries out for a nested loop.

Cries out for a *function*, I'd say.

> 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.

> import zipfile
> import os

> 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'
> ]

def zipit(outfile, file_list):
     zFile = zipfile.ZipFile(zips[0], 'w')
     for file in file_list:
         zFile.write(file, os.path.basename(file), zipfile.ZIP_DEFLATED)
     zFile.close()

zipit("c:/spare.zip", spare)
zipit("c:/seekfacts.zip", seekfacts)

</F>




More information about the Python-list mailing list