For_loops hurt my brain.

B

bsagert

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()
 
D

Dan

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
 
F

Fredrik Lundh

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

mizhi

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.basename(f), zipfile.ZIP_DEFLATED);

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


for zipfilename,files in zips.items():
zipit(zipfilename, files);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top