I’m using Python 2.5 and trying to make a variable called total and adding three files to it. Then i compress total and convert it into hex and split it into a list of strings with maximum length of 4096 characters.
Currently I have a list of the files and i’m concatenating it all to total in this way
filelist = ['debug.log', 'error.log', 'reclog.log'];
total = ''
for files in filelist:
f = open(files, 'r');
total = total + f.read();
f.close();
compressedtotal = zlib.compress(total);
hextotal = compressedtotal.encode('hex');
Upto here I feel i did it efficiently. But then i try to split hextotal into a list of strings called msglist. But the maximum length of each string in msglist should be no more than 4096 characters long.
if len(hextotal)%4096 >0 : checker = 1;
else: checker = 0;
nmsgs = int(math.ceil(len(hextotal)/4096));
nn = str(nmsgs);
msglist = [];
for msgs in range(1,nmsgs+1):
if msgs == nmsgs and checker == 1:
msglist.append(hextotal[4096*(msgs-1):]);
else: msglist.append(hextotal[4096*(msgs-1):4096*(msgs)]);
There should me a more simpler way because this isnt “pythonic” and im quite new to python.
THanks a lot
My personal favorite is a list comprehension: