-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard.py
executable file
·43 lines (34 loc) · 967 Bytes
/
shard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
# shard.py
# Copyright (c) 2013 Mark Raymond
# Released under the MIT license
import sys
if len(sys.argv) != 3:
print('Usage: shard INPUT CHUNKS', file=sys.stderr)
print('Shards INPUT line by line into CHUNKS files.', file=sys.stderr)
exit(1)
filename = sys.argv[1]
chunks = sys.argv[2]
try:
file = open(filename, 'rt')
except OSError:
print('Could not open file "', filename, '"', sep='', file=sys.stderr)
exit(1)
try:
chunks = int(chunks)
except ValueError:
print('"', chunks, '" is not a valid integer', sep='', file=sys.stderr)
exit(1)
if (chunks < 2):
print('Number of chunks must be at least 2', file=sys.stderr)
exit(1)
format = filename + '.%0' + str(len(str(chunks - 1))) + 'd'
try:
out = list(map(lambda i: open(format % i, 'wt'), range(chunks)))
except OSError:
print('Could not open all output files', file=sys.stderr)
exit(1)
i = 0
for line in file:
out[i].write(line)
i = (i + 1) % chunks