The Python Challenge – 6

This was fun, using python to access zip files. I based it mostly on #4 with a small twist at the end to print out the final answer. The obvious answer didn’t work – I thought I was looking at challenge 7 but realised after banging my head against the wall for a while that I was still on number 6. What was really annoying was that I had already noticed the correct word whilst fiddling with the code.

****************************************************************
****************************************************************
** **
** OO OO XX YYYY GG GG EEEEEE NN NN **
** OO OO XXXXXX YYYYYY GG GG EEEEEE NN NN **
** OO OO XXX XXX YYY YY GG GG EE NN NN **
** OOOOOOOO XX XX YY GGG EEEEE NNNN **
** OOOOOOOO XX XX YY GGG EEEEE NN **
** OO OO XXX XXX YYY YY GG GG EE NN **
** OO OO XXXXXX YYYYYY GG GG EEEEEE NN **
** OO OO XX YYYY GG GG EEEEEE NN **
** **
****************************************************************
**************************************************************

2.7 Solution

#zip?
import zipfile, re, sys
data = zipfile.ZipFile("channel.zip")
#data is now some sort of object
#data.namelist() is all the files in the archive

count = 1
final = []

def getNextCode(lastCode):
while True:
try:
myfile = data.open(lastCode + '.txt')
myinfo = data.getinfo(lastCode + '.txt')
final.append(myinfo.comment)
code = re.findall('(?<=Next nothing is )\w*',myfile.read())
#print "Code %i is %s. \nPage read was '%s.txt'\n" % (count, code[0], lastCode)
#print "Code %i\n" % (count)
return code
except (IndexError, NameError):
print "Error" #: Page message was '%s'\nPage was: '%s.txt'." % (page,lastCode)
return False

nextCode = getNextCode('90052')

for count in range (1,len(data.namelist())- 2):
nextCode = getNextCode(nextCode[0])


myfile = data.open(nextCode[0] + '.txt').read()

#Collect the comments.
for character in final:
sys.stdout.write(character)
#look at the message and think about it...

3.2.3 Solution 

#zip?
import zipfile, re, sys
data = zipfile.ZipFile("channel.zip")
#data is now some sort of object
#data.namelist() is all the files in the archive

count = 1
final = []

def getNextCode(lastCode):
while True:
try:
myfile = data.open(lastCode + '.txt')
myinfo = data.getinfo(lastCode + '.txt')
final.append(myinfo.comment)
code = re.findall(r'(?<=Next nothing is )\w*',myfile.read().decode("utf-8"))
return code
except (IndexError, NameError):
print ("Error")
return False

nextCode = getNextCode('90052')

for count in range (1,len(data.namelist())- 2):
nextCode = getNextCode(nextCode[0])


myfile = data.open(nextCode[0] + '.txt').read()

#Collect the comments.
for character in final:
sys.stdout.write(character)