Python - How can I open a file and specify the offset in bytes?

前端 未结 8 1693
我在风中等你
我在风中等你 2020-12-17 14:37

I\'m writing a program that will parse an Apache log file periodically to log it\'s visitors, bandwidth usage, etc..

The problem is, I don\'t want to open the log an

8条回答
  •  长情又很酷
    2020-12-17 15:25

    Here is an efficient and safe snippet to do that saving the offset read in a parallell file. Basically logtail in python.

    with open(filename) as log_fd:
        offset_filename = os.path.join(OFFSET_ROOT_DIR,filename)
        if not os.path.exists(offset_filename):
            os.makedirs(os.path.dirname(offset_filename))
            with open(offset_filename, 'w') as offset_fd:
                offset_fd.write(str(0))
        with open(offset_filename, 'r+') as offset_fd:
            log_fd.seek(int(offset_fd.readline()) or 0)
            new_logrows_handler(log_fd.readlines())
            offset_fd.seek(0)
            offset_fd.write(str(log_fd.tell()))
    

提交回复
热议问题