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

后端 未结 3 867
渐次进展
渐次进展 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:42

    yield is the keyword in python used for generator expressions. That means that the next time the function is called (or iterated on), the execution will start back up at the exact point it left off last time you called it. The two functions behave identically; the only difference is that the first one uses a tiny bit more call stack space than the second. However, the first one is far more reusable, so from a program design standpoint, the first one is actually better.

    EDIT: Also, one other difference is that the first one will stop reading once all the data has been read, the way it should, but the second one will only stop once either f.read() or process_data() throws an exception. In order to have the second one work properly, you need to modify it like so:

    f = open(file, 'rb')
    while True:
        piece = f.read(1024)  
        if not piece:
            break
        process_data(piece)
    f.close()
    

提交回复
热议问题