-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile-split.py
More file actions
executable file
·40 lines (34 loc) · 1.05 KB
/
file-split.py
File metadata and controls
executable file
·40 lines (34 loc) · 1.05 KB
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
# -*- coding: cp936 -*-
import os
import time
def mkSubFile(lines, head, srcName, sub):
[des_filename, extname] = os.path.splitext(srcName)
filename = des_filename + '_' + str(sub) + extname
print('make file: %s' % filename)
fout = open(filename, 'w', encoding='UTF-8')
try:
fout.writelines([head])
fout.writelines(lines)
return sub + 1
finally:
fout.close()
def splitByLineCount(filename, count):
fin = open(filename, 'r', encoding='UTF-8')
try:
head = fin.readline()
buf = []
sub = 1
for line in fin:
buf.append(line)
if len(buf) == count:
sub = mkSubFile(buf, head, filename, sub)
buf = []
if len(buf) != 0:
sub = mkSubFile(buf, head, filename, sub)
finally:
fin.close()
if __name__ == '__main__':
begin = time.time()
splitByLineCount('./data/catalina.out', 60000)
end = time.time()
print('time is %d seconds ' % (end - begin))