OILS / devtools / tsv_concat.py View on Github | oilshell.org

34 lines, 24 significant
1#!/usr/bin/env python2
2from __future__ import print_function
3"""
4tsv_concat.py - Also run with Python 3
5"""
6import sys
7
8def main(argv):
9 first_header = None
10 for path in argv[1:]:
11 with open(path) as f:
12 # Assume there's no quoting or escaping.
13 header = f.readline()
14 if first_header is None:
15 sys.stdout.write(header)
16 first_header = header.strip()
17 else:
18 h = header.strip()
19 if h != first_header:
20 raise RuntimeError(
21 'Invalid header in %r: %r. Expected %r' % (path, h,
22 first_header))
23 # Now print rest of lines
24 for line in f:
25 sys.stdout.write(line)
26
27
28
29if __name__ == '__main__':
30 try:
31 main(sys.argv)
32 except RuntimeError as e:
33 print('FATAL: %s' % e, file=sys.stderr)
34 sys.exit(1)