For_loops hurt my brain.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bsagert@gmail.com

    For_loops hurt my brain.

    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_zip ped (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(fil es, os.path.basenam e(files), zipfile.ZIP_DEF LATED)
    zFile.close()

    zFile = zipfile.ZipFile (zips[1], 'w')
    for files in seekfacts:
    zFile.write(fil es, os.path.basenam e(files), zipfile.ZIP_DEF LATED)
    zFile.close()

  • Dan

    #2
    Re: For_loops hurt my brain.

    On Jul 16, 1:42 pm, bsag...@gmail.c om 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_zip ped (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(fil es, os.path.basenam e(files), zipfile.ZIP_DEF LATED)
    zFile.close()
    >
    zFile = zipfile.ZipFile (zips[1], 'w')
    for files in seekfacts:
    zFile.write(fil es, os.path.basenam e(files), zipfile.ZIP_DEF LATED)
    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(fna me, os.path.basenam e(files),
    zipfile.ZIP_DEF LATED)
    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

    Comment

    • Fredrik Lundh

      #3
      Re: For_loops hurt my brain.

      bsagert@gmail.c om 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_zip ped (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(fil e, os.path.basenam e(file), zipfile.ZIP_DEF LATED)
      zFile.close()

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

      </F>

      Comment

      • mizhi

        #4
        Re: For_loops hurt my brain.

        Other possibility, combining Dan and Fredrik's posts:

        import zipfile
        import os

        zips = {
        'c:/spare.zip': ['c:/spare/huge.fm3', 'c:/spare/huge.wk3'],
        'c:/seekfacts.zip': ['c:/seekfacts/bookmark.html', 'c:/seekfacts/
        index.htm', 'c:/seekfacts/seek.css', 'c:/seekfacts/seek.js']
        };


        def zipdir(zFile, files):
        for f in files:
        zFile.write(f, os.path.basenam e(f), zipfile.ZIP_DEF LATED);

        def zipit(zipfilena me, files):
        zFile = zipfile.ZipFile (zipfilename, 'w');
        zipdir(zFile, files);
        zFile.close();


        for zipfilename,fil es in zips.items():
        zipit(zipfilena me, files);

        Comment

        Working...