How to read a large file - line by line?

前端 未结 11 862
一整个雨季
一整个雨季 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:00

    Need to frequently read a large file from last position reading ?

    I have created a script used to cut an Apache access.log file several times a day. So I needed to set a position cursor on last line parsed during last execution. To this end, I used file.seek() and file.seek() methods which allows the storage of the cursor in file.

    My code :

    ENCODING = "utf8"
    CURRENT_FILE_DIR = os.path.dirname(os.path.abspath(__file__))
    
    # This file is used to store the last cursor position
    cursor_position = os.path.join(CURRENT_FILE_DIR, "access_cursor_position.log")
    
    # Log file with new lines
    log_file_to_cut = os.path.join(CURRENT_FILE_DIR, "access.log")
    cut_file = os.path.join(CURRENT_FILE_DIR, "cut_access", "cut.log")
    
    # Set in from_line 
    from_position = 0
    try:
        with open(cursor_position, "r", encoding=ENCODING) as f:
            from_position = int(f.read())
    except Exception as e:
        pass
    
    # We read log_file_to_cut to put new lines in cut_file
    with open(log_file_to_cut, "r", encoding=ENCODING) as f:
        with open(cut_file, "w", encoding=ENCODING) as fw:
            # We set cursor to the last position used (during last run of script)
            f.seek(from_position)
            for line in f:
                fw.write("%s" % (line))
    
        # We save the last position of cursor for next usage
        with open(cursor_position, "w", encoding=ENCODING) as fw:
            fw.write(str(f.tell()))
    

提交回复
热议问题