Search code examples
pythonlaspy

Iterate over individual bytes then save it into a file without alternating the content


I have a byte string returned from API and store in response.content

With small content, I can save it into a file with no problem using the following code

with open(save_path, 'wb') as save_file:
    save_file.write(response.content)

But for the larger file, it will cause Memory Error, So I tried not to read the content all at once, by using this code

with open(save_path, 'wb') as save_file:
    for x in response.content: 
        save_file.write(bytes(x)) #the x from iteration seem to be converted to int so I convert it back

But the method above seems to alternate the content because it doesn't compatible with another library anymore (In my when Laspy try to read the saved file, laspy.util.LaspyException: Invalid format: h0.0 error appears)

How can I do it?


Solution

  • I see your problem on using bytes(x). change it to x.to_bytes(1, 'big') solve your problem

    Use below code to reveal what difference

    a = b'\xcf\x84o\xcf\x81\xce\xbdo\xcf\x82'
    a.decode('utf-8') # τoρνoς
    
    with open('./save.txt', 'wb') as save_file:
        for i in a:
            print(i.to_bytes(1, 'big')) # write it to file, not the others
            print(i)
            print(bytes(i))
            print('----')
    

    enter image description here