Lazy Method for Reading Big File in Python?

前端 未结 12 1777
谎友^
谎友^ 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:21

    you can use following code.

    file_obj = open('big_file') 
    

    open() returns a file object

    then use os.stat for getting size

    file_size = os.stat('big_file').st_size
    
    for i in range( file_size/1024):
        print file_obj.read(1024)
    

提交回复
热议问题