How to read the end of the file?

Such as the title, in reading an access log (file has been written), the file is small, with the following code. But when the file on the G, each cycle of the whole file again a bit unrealistic, seek solutions. Research on seek (), or don't understand

  1. import datetime

  2. t1=datetime.datetime.now()
  3. t2=t1+datetime.timedelta(minutes=-1)
  4. pasttime=t2.strftime('%d/%b/%Y:%H:%M:')

  5. log_path="/opt/log/access.log"
  6. list=[]
  7. findfile=open(log_path)
  8. for i in findfile.readlines():
  9. line=i.strip()
  10. if pasttime in line:
  11. nem=line.split(" ")[9]
  12. nem=int(nem)
  13. list.append(nem)
  14. #print "%s byte" %sum(list) #byte
  15. print "%s Mbps" %(float(sum(list)) / 1024 / 1024 * 8) #Mb
  16. findfile.close()

Started by Fiona at February 11, 2016 - 12:26 AM

Is this your want to know a few lines, there are still a few characters?

I think you need to function can be achieved with List...

len(open(file).readlines())

So on the line!

Posted by Lorin at February 23, 2016 - 12:31 AM

Seek can be

In the impression of a one parameter is determined to move forward and backward

Posted by Abner at March 04, 2016 - 12:49 AM

I wrote two simple functions, for example, but for anomaly detection what did not add. Only basic functions.
  1. def read_from_specified_position(filename):
  2. '''Demonstrate how to read from specified offset.'''
  3. # for better reading, recommend to record previous offset with file.tell()
  4. # for demonstrate, just try to read from a random position
  5. import os
  6. import random
  7. file_handle = open(filename)
  8. stat = os.fstat(file_handle.fileno())
  9. offset = random.randint(0, stat.st_size)
  10. file_handle.seek(offset, os.SEEK_SET)
  11. membuffer = file_handle.read(1024)
  12. return membuffer, file_handle.tell()

  13. def read_from_tail(filename):
  14. '''Demonstrate how to read from tail.'''
  15. import os
  16. import cStringIO
  17. file_handle = open(filename)
  18. file_handle.seek(-1024, os.SEEK_END)
  19. membuffer = cStringIO.StringIO()
  20. membuffer.write(file_handle.read(1024))
  21. return membuffer

  22. def main():
  23. obj = read_from_tail("WindowsUpdate.log")
  24. print(obj.getvalue())
  25. obj.close()

  26. print ("*" * 80)

  27. obj, offset = read_from_specified_position("WindowsUpdate.log")
  28. print("Read content:\n%r" % obj)
  29. print("After reading, the file offset is: {pos}".format(pos=offset))

Posted by Lareina at March 14, 2016 - 1:45 AM

From official documents
  1. seek(offset, whence=SEEK_SET)
  2. Change the stream position to the given byte offset. offset is interpreted relative to the position indicated by whence. Values for whence are:

  3. SEEK_SET or 0 – start of the stream (the default); offset should be zero or positive
  4. SEEK_CUR or 1 – current stream position; offset may be negative
  5. SEEK_END or 2 – end of the stream; offset is usually negative
  6. Return the new absolute position.

  7. New in version 3.1: The SEEK_* constants.

  8. New in version 3.3: Some operating systems could support additional values, like os.SEEK_HOLE or os.SEEK_DATA. The valid values for a file could depend on it being open in text or binary mode.

Posted by Arno at January 11, 2017 - 11:41 PM

Juye brother wrote a
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. # Filename: test.py
  4. # Last modified: 2013-12-01 16:59
  5. # Author: xiujie_jiang
  6. # Description:

  7. import sys
  8. import time
  9. import datetime

  10. def main(log_file):
  11. count = 0
  12. t1 = datetime.datetime.now()
  13. t2 = t1+datetime.timedelta(minutes=-1)
  14. pasttime = t2.strftime('%d/%b/%Y:%H:%M:')

  15. with open(log_file, 'r') as f:
  16. f.seek(0,1)
  17. while True:
  18. current_position = f.tell()
  19. line = f.readline()
  20. if not line:
  21. f.seek(current_position)
  22. else:
  23. if pasttime in line:
  24. content = line.split(" ")[9]
  25. count += int(content)
  26. sys.stdout.write('\rMbps: %s\r' % (float(count/1024/1024*8)))
  27. sys.stdout.flush()
  28. #time.sleep(0.5) //

  29. if __name__ == '__main__':
  30. main("/opt/log/access.log")

Posted by Lulu at January 13, 2017 - 11:32 PM