Read file in chunks - RAM-usage, read Strings from binary files

后端 未结 3 855
渐次进展
渐次进展 2020-12-05 19:58

i\'d like to understand the difference in RAM-usage of this methods when reading a large file in python.

Version 1, found here on stackoverflow:

def          


        
3条回答
  •  有刺的猬
    2020-12-05 20:44

    I think probably the best and most idiomatic way to do this would be to use the built-in iter() function with a sentinel value to create and use an iterable as shown below. Note that the last chunk might be less that the requested chunk size if the file size isn't an exact multiple of it.

    from functools import partial
    
    CHUNK_SIZE = 1024
    filename = 'testfile.dat'
    
    with open(filename, 'rb') as file:
        for chunk in iter(partial(file.read, CHUNK_SIZE), b''):
            process_data(chunk)
    

    Update: Don't know when it was added, but almost exactly what's above is in now shown as an example in the official documentation of the iter() function.

提交回复
热议问题