How can I read large text files in Python, line by line, without loading it into memory?

前端 未结 15 1303
臣服心动
臣服心动 2020-11-22 03:32

I need to read a large file, line by line. Lets say that file has more than 5GB and I need to read each line, but obviously I do not want to use readlines() bec

15条回答
  •  臣服心动
    2020-11-22 04:16

    An old school approach:

    fh = open(file_name, 'rt')
    line = fh.readline()
    while line:
        # do stuff with line
        line = fh.readline()
    fh.close()
    

提交回复
热议问题