-
Notifications
You must be signed in to change notification settings - Fork 0
/
shuffle.py
48 lines (30 loc) · 884 Bytes
/
shuffle.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
import os
import sys
import random
import tempfile
from subprocess import call
def main(files, temporary=False):
fds = [open(ff) for ff in files]
lines = []
for l in fds[0]:
line = [l.strip()] + [ff.readline().strip() for ff in fds[1:]]
lines.append(line)
[ff.close() for ff in fds]
random.shuffle(lines)
if temporary:
fds = []
for ff in files:
path, filename = os.path.split(os.path.realpath(ff))
fds.append(tempfile.TemporaryFile(prefix=filename+'.shuf', dir=path))
else:
fds = [open(ff+'.shuf','w') for ff in files]
for l in lines:
for ii, fd in enumerate(fds):
print >>fd, l[ii]
if temporary:
[ff.seek(0) for ff in fds]
else:
[ff.close() for ff in fds]
return fds
if __name__ == '__main__':
main(sys.argv[1:])