Advertisement
AzraelNewtype

frames-to-spans.py

Nov 5th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. from os.path import splitext
  5.  
  6. def write_range(start, end, handle):
  7.     if start == end:
  8.         handle.write("{0}\n".format(start))
  9.     elif end < 0:
  10.         pass
  11.     else:
  12.         handle.write("{0},{1}\n".format(start, end))
  13.  
  14. try:
  15.     file_in = sys.argv[1]
  16. except IndexError:
  17.     print("Usage: {0} [file with list of combed frames]".format(sys.argv[0]))
  18.     raise SystemExit
  19.  
  20. last = -9
  21. start = 0
  22.  
  23. fparts = splitext(file_in)
  24. file_out = "{0}-range{1}".format(fparts[0], fparts[1])
  25.  
  26. try:
  27.     with open(file_in) as in_f:
  28.         try:
  29.             with open(file_out, 'w') as o:
  30.                 for line in in_f:
  31.                     curr = int(line)
  32.                     if curr - last > 4:
  33.                         write_range(start, last, o)
  34.                         start = curr
  35.                     last = curr
  36.                 write_range(start, last, o)
  37.         except IOError:
  38.             print("Cannot open {0} for writing.".format(file_out))
  39.             raise SystemExit
  40. except IOError:
  41.     print("Cannot open: {0}".format(file_in))
  42.     raise SystemExit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement