I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into an
In Python 3.8+ you can use .read() in a while
loop:
with open("somefile.txt") as f:
while chunk := f.read(8192):
do_something(chunk)
Of course, you can use any chunk size you want, you don't have to use 8192
(2**13
) bytes. Unless your file's size happens to be a multiple of your chunk size, the last chunk will be smaller than your chunk size.