473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Jul 16 '08 #1
3 921
Dan
On Jul 16, 1:42 pm, bsag...@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. 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
Jul 16 '08 #2
bs*****@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>

Jul 16 '08 #3
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);
Jul 16 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: brendan | last post by:
here's a brain teaser for someone with more skills or at least more lateral thinking capability than me - done my nut over this one... have written a list manager in PHP which a) posts out new...
2
by: Daniel Bickett | last post by:
I was reading the "Pickling and inheritance are making me hurt" thread, and the latest suggestion (as of this posting) was to do with the __setstate__ and __getstate__ methods. They caught my...
7
by: robert | last post by:
running 8.1.7 server, 8.1.6 client. i *thought* inner join should not return nulls, but not only that, but i get way more rows than i'm expecting. assume: order table: order_number
2
by: Doc | last post by:
Per earlier post, I am trying to save 'out' production data from a program called Solomon - basically (I think) this was /is an Access/Sql based program. We are updating to different application...
7
by: One Handed Man \( OHM - Terry Burns \) | last post by:
I've been battling with this stupid problem for hours now. WebApp: Trying to do a simple transformation using XSLT to a Web Page, but it just failes without an error message ( In other words,...
3
by: RC | last post by:
Dear Dudes, I post this in multiple groups for opening brain storm. Sometime I need to query the data from database server then display them into user's browser in HTML <table>. But if the...
15
by: Chung Leong | last post by:
Here's a little brain teaser distilled from a bug that took me a rather long time to figure out. The two functions in the example below behave differently. The difference is easy to spot, of...
7
by: Mark A | last post by:
If server 01 running HADR in the primary role crashes, and the DBA does a HADR takeover by force on the 02 server to switch roles, then the 02 server is now the primary. What happens when the...
2
by: oorga.power | last post by:
Our 90 % percent work based on our mind. all the successful person of this world won from mind. C Langugae is great language, it needs logics. Logics can only give your logical part of brain,...
0
by: DotComTr | last post by:
Hi, I want to write an Bot which could play games in a site automatically. the site is www.facebook.com. Who Has Big Brain game. I would like to write a VB Bot which will play game of Who has big...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.