How to read a large file - line by line?

前端 未结 11 1025
一整个雨季
一整个雨季 2020-11-21 11:44

I want to iterate over each line of an entire file. One way to do this is by reading the entire file, saving it to a list, then going over the line of interest. This method

11条回答
  •  Happy的楠姐
    2020-11-21 12:01

    this is a possible way of reading a file in python:

    f = open(input_file)
    for line in f:
        do_stuff(line)
    f.close()
    

    it does not allocate a full list. It iterates over the lines.

提交回复
热议问题