Open and read all binary files from directory an output to lists – Python

Question:

I’m trying to output the entire contents of multiple .pkt files, containing binary data, as a series of hexadecimal lists: (Adapted from How to open every file in a folder)

import glob
path = 'filepath'
for filename in glob.glob(os.path.join(path, '*.pkt')):    
   with open(os.path.join(os.getcwd(), filename), 'rb') as f:
   pair_hex = ["{:02x}".format(c) for c in f.read()]
   print(pair_hex)

Which outputs:

['09', '04', '04', '04', '04', '04', '04', '04', '04', '0b', '09']
['09', '04', 'bb']
['09', 'bb']
['bb']
['09', '04', '0b', '09']

That makes sense, because i’m looping through the files, but what I need is:

[['09', '04', '04', '04', '04', '04', '04', '04', '04', '0b', '09'],['09', '04', 'bb'],['09', 'bb'],['bb'],['09', '04', '0b', '09']]

So I can manipulate all the data.

I have tried to apply append(), "".join(), map() as well as the advice at How to merge multiple lists into one list in python?, but nothing changes the output. How can I get the desired list of lists?

Asked By: red_sach

||

Answers:

untested but try

import glob, os
path = 'filepath'
ret = []
for filename in glob.glob(os.path.join(path, '*.pkt')):    
    with open(os.path.join(os.getcwd(), filename), 'rb') as f:
        pair_hex = ["{:02x}".format(c) for c in f.read()]
        ret.append(pair_hex)

print(ret)

the above prints the following on my console which is the same as your "desired output"

[['09', '04', '04', '04', '04', '04', '04', '04', '04', '0b', '09'], ['09', '04', 'bb'], ['09', 'bb'], ['bb'], ['09', '04', '0b', '09']]

and this is what I used to create the .pkt files on my machine with out set to a copy–paste of your "desired output"

out = [['09', '04', '04', '04', '04', '04', '04', '04', '04', '0b', '09'],['09', '04', 'bb'],['09', 'bb'],['bb'],['09', '04', '0b', '09']]

for i, a in enumerate(out):
    with open(f"{i}.pkt", 'w') as f:
        f.write(''.join(map(lambda s: chr(int(s, 16)), a)))

Answered By: user5386938
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.