-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfiles2Group.py
More file actions
74 lines (69 loc) · 2.37 KB
/
files2Group.py
File metadata and controls
74 lines (69 loc) · 2.37 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python
# -*- conding:UTF-8 -*-
import os
import sys
import argparse
def parse_args():
usage = "python files2Group.py -d pathToDir -k fileNameKeyWord"
parser = argparse.ArgumentParser(usage=usage)
parser.add_argument('-k', dest='key', metavar='filename key', required=True,help="The keyword of file name")
parser.add_argument('-d', dest='path', metavar='pathto dir', default='.', help="The file directory. Default is the current directory")
return parser.parse_args()
def merge(filePath,keyword):
filelist=[]
if os.path.isdir(filePath):
if os.listdir(filePath):
filelist.extend(os.listdir(filePath))
else:
print("The dir is empty")
sys.exit()
else:
print('The dir is not exist')
sys.exit()
filelist=[x for x in filelist if keyword in x]
start=0
end=20
tmp=[]
index=1
tag=1
while tag:
if end < len(filelist):
tmp=filelist[start:end]
f_write=open(filePath+'/'+'group'+str(index)+'.stat.xls','w')
head=open(filePath+'/'+tmp[0],'r').readlines()[0].split('\t')
head=head[0]+'\t'+head[3]+'\t'+head[4]+'\t'+head[5]
f_write.write(head)
for file in tmp:
f_read=open(filePath+'/'+file,'r')
context=f_read.readlines()[1:]
for i in context:
i=i.split('\t')
i=i[0]+'\t'+i[3]+'\t'+i[4]+'\t'+i[5]
f_write.write(i)
else:
tmp=filelist[start:len(filelist)]
f_write=open(filePath+'/'+'group'+str(index)+'.stat.xls','w')
head=open(filePath+'/'+tmp[0],'r').readlines()[0].split('\t')
head=head[0]+'\t'+head[3]+'\t'+head[4]+'\t'+head[5]
f_write.write(head)
for file in tmp:
f_read=open(filePath+'/'+file,'r')
context=f_read.readlines()[1:]
for i in context:
i=i.split('\t')
i=i[0]+'\t'+i[3]+'\t'+i[4]+'\t'+i[5]
f_write.write(i)
tag=0
f_write.close()
f_read.close()
start+=20
end+=20
index+=1
def main():
args = parse_args()
if args.key and args.path:
path=args.path
key= args.key
merge(path,key)
if __name__ == "__main__":
main()