Can't use .readinto on zlib.DecompIO: TypeError: object with buffer protocol required #9291
-
The following code fails (only relevant bits left in): def open_dir(path):
files = []
for _file in os.listdir(path):
file = path + '/' + _file
if file.endswith('.bin'):
os.remove(file)
for _file in os.listdir(path):
print(_file)
filename = path + '/' + _file
if filename.endswith('.zl'):
file_out = filename.replace('.zl', '.bin')
print(file_out)
decompress_file(filename, file_out)
files.append(file_out)
(.....)
def decompress_file(in_path, out_path):
with open(in_path, 'rb') as infile, open(out_path, 'wb') as outfile:
decompio = zlib.DecompIO(infile)
decompio.readinto(outfile) Returning the following traceback:
The code here fails on the It seems that the usual fix for this problem is to "use strings/bytes instead of passed object", but this doesn't make sense here as all of these objects should have the buffer protocol implemented. I checked the types for It's worth noting that I can use the Am I doing something wrong, or did I hit some bizzare bug? Could it be an out-of-memory error in disguise? I'm using a Raspberry Pi Pico RP2040, with |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Yes that is the correct way to do it, create a temporary
There is a difference between "buffer protocol" and "stream protocol". The former is an in-memory object like I don't think CPython even allows reading directly from one file/stream into another? |
Beta Was this translation helpful? Give feedback.
Yes that is the correct way to do it, create a temporary
bytearray
to buffer the data from one stream to another.There is a difference between "buffer protocol" and "stream protocol". The former is an in-memory object like
str
,bytes
,bytearray
(mutable),memoryview
and some other things. The latter is an object that hasread
/write
methods (essentially inherits fromIOBase
).I don't think CPython even allows reading directly from one file/stream into another?