How to read a large file - line by line?

前端 未结 11 1022
一整个雨季
一整个雨季 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条回答
  •  借酒劲吻你
    2020-11-21 12:10

    From the python documentation for fileinput.input():

    This iterates over the lines of all files listed in sys.argv[1:], defaulting to sys.stdin if the list is empty

    further, the definition of the function is:

    fileinput.FileInput([files[, inplace[, backup[, mode[, openhook]]]]])
    

    reading between the lines, this tells me that files can be a list so you could have something like:

    for each_line in fileinput.input([input_file, input_file]):
      do_something(each_line)
    

    See here for more information

提交回复
热议问题