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

前端 未结 8 1683
我在风中等你
我在风中等你 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:38

    log = open('myfile.log')
    pos = open('pos.dat','w')
    print log.readline()
    pos.write(str(f.tell())
    log.close()
    pos.close()
    
    log = open('myfile.log')
    pos = open('pos.dat')
    log.seek(int(pos.readline()))
    print log.readline()
    

    Of course you shouldn't use it like that - you should wrap the operations up in functions like save_position(myfile) and load_position(myfile), but the functionality is all there.

提交回复
热议问题