Lazy Method for Reading Big File in Python?

前端 未结 12 1969
谎友^
谎友^ 2020-11-21 06:40

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

12条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-21 07:00

    If your computer, OS and python are 64-bit, then you can use the mmap module to map the contents of the file into memory and access it with indices and slices. Here an example from the documentation:

    import mmap
    with open("hello.txt", "r+") as f:
        # memory-map the file, size 0 means whole file
        map = mmap.mmap(f.fileno(), 0)
        # read content via standard file methods
        print map.readline()  # prints "Hello Python!"
        # read content via slice notation
        print map[:5]  # prints "Hello"
        # update content using slice notation;
        # note that new content must have same size
        map[6:] = " world!\n"
        # ... and read again using standard file methods
        map.seek(0)
        print map.readline()  # prints "Hello  world!"
        # close the map
        map.close()
    

    If either your computer, OS or python are 32-bit, then mmap-ing large files can reserve large parts of your address space and starve your program of memory.

提交回复
热议问题