forked from zygmuntz/phraug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk.py
47 lines (28 loc) · 745 Bytes
/
chunk.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
44
45
46
47
'''
split a file into a given number of chunks randomly, line by line.
Usage: chunk.py <input file> <number of chunks> [<seed>]'
'''
import sys, random, os
input_file = sys.argv[1]
num_chunks = int( sys.argv[2] )
try:
seed = sys.argv[3]
except IndexError:
seed = None
if seed:
print "seeding: %s" % ( seed )
random.seed( seed )
basename = os.path.basename( input_file )
basename, ext = os.path.splitext( basename )
i = open( input_file )
os = {}
for n in range( num_chunks ):
output_file = "%s_%s%s" % ( basename, n, ext )
os[n] = open( output_file, 'wb' )
counter = 0
for line in i:
n = random.randint( 0, num_chunks - 1 )
os[n].write( line )
counter += 1
if counter % 100000 == 0:
print counter