Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. lcz -- estimate line-count of gzip file
  5. Intended for log files with regular line lengths and content.
  6.  
  7. Usage: lcz FILE
  8.  
  9. """
  10. import gzip
  11. import sys
  12. import textwrap
  13. import zlib
  14.  
  15. SAMPLE_SIZE = 51200
  16. z = zlib.decompressobj(-zlib.MAX_WBITS)
  17.  
  18. try:
  19. with gzip.open(sys.argv[1], 'r') as f:
  20. f._init_read()
  21. f._read_gzip_header()
  22. zchunk = f.fileobj.read(SAMPLE_SIZE)
  23. f.fileobj.seek(0, 2)
  24. file_size = f.fileobj.tell()
  25. except IndexError:
  26. sys.exit(textwrap.dedent(__doc__).strip())
  27. except IOError as e:
  28. sys.exit(textwrap.dedent(__doc__).strip() + '\n' + str(e))
  29. else:
  30. chunk = z.decompress(zchunk)
  31. zbytes_per_line = float(SAMPLE_SIZE) / chunk.count('\n')
  32. print(file_size / zbytes_per_line)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement