- Code: Select all
#!/usr/bin/python3
import zipfile
import re
zfile = zipfile.ZipFile('ziparchive.zip', 'r')
all_indexes = []
file_contents = zfile.read('90052.txt')
while True:
match = re.findall(b'\d+', file_contents)
# break if there are no numbers so the length of the 'match' list is 0
if len(match) == 0:
break
index_file = match[0].decode("utf-8")
next_file = index_file + ".txt"
all_indexes.append(next_file)
file_contents = zfile.read(next_file)
print(file_contents)
print('.'.join([zfile.getinfo(i).comment.decode("utf-8") for i in all_indexes]))
Could you have a look at it and suggest any improvements? I mean it works okay but I'm far from expert and not sure if I'm not doing something in a clumsy and/or not recommended way.
Also, it it really necessary to decode it from bytes to strings in 2 places? Would not it possible to do it once?
Thank you.