cat table.py——制作表模板
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 import os
4
5 def get_length(ts):
6 ls = []
7 hs = ts['head']
8 for h in hs:
9 ls.append(len(h))
10 bs = ts['body']
11 for b in bs:
12 for i in range(len(ls)):
13 li = len(b[i])
14 if ls[i] < li:
15 ls[i] = li
16 return ls
17
18 def out_line(ts):
19 rs = '+'
20 ls = get_length(ts)
21 for l in ls:
22 rs += '-'*(l+2)+'+'
23 rs += os.linesep
24 return rs
25
26 def out_head(ts):
27 rs = '|'
28 ls = get_length(ts)
29 hs = ts['head']
30 for i in range(len(ls)):
31 rs += ' '+hs[i]+' '*(ls[i]-len(hs[i]))+' |'
32 rs += os.linesep
33 return rs
34
35 def out_body(ts):
36 rs = ''
37 ls = get_length(ts)
38 bs = ts['body']
39 for i in bs:
40 r = '|'
41 for j in range(len(ls)):
42 r += ' '+i[j]+' '*(ls[j]-len(i[j]))+' |'
43 rs += r + os.linesep
44 return rs
45
46 def out_table(ts):
47 if 'head' in ts and 'body' in ts
48 and len(ts['head']) == 0:
49 return ''
50 rs = out_line(ts)
51 rs += out_head(ts)
52 rs += out_line(ts)
53 rs += out_body(ts)
54 rs += out_line(ts)
55 return rs
56
57 if __name__ == '__main__':
58 T = {
59 'head': [ 'User', 'Host', 'Password', 'tip'],
60 'body': [
61 ['root', 'localhost', 'abc', '' ],
62 ['andy', '127.0.0.1', '1234567890', '' ],
63 ['tim', '111.67.192.108', '', '' ],
64 ]
65 }
66 print(out_table(T), end='')
cat zero.py——调用table表模板,然后制作文件分类管理器
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 import table
4 import sys
5 import os, os.path
6
7 def get_help():
8 rs = 'Usage:' + os.linesep +
9 ' zero {-a|--author} show author of this application.' + os.linesep +
10 ' zero {-c|--copyright} show copyright of this application.' + os.linesep +
11 ' zero {-h|--help} show this help and exit.' + os.linesep +
12 ' zero [-m] <PATH> show all files of class in sepcified PATH.'
13 return rs
14
15 def get_copyright():
16 rs = '(c)2011-2018 kevin@stupig.com all rights reserved'
17 return rs
18
19 def get_author():
20 rs = 'Author: kevin'
21 return rs
22
23 def get_version():
24 return '1.0.0'
25
26 def get_table(path):
27 hs = []
28 bs = []
29 # foo/1.txt
30 # foo/1.png
31 # foo/directory
32 # foo/abc
33 fs = os.listdir(path)
34 afs = []
35 for f in fs:
36 if os.path.isdir(os.path.join(path, f)):
37 continue
38 afs.append(f)
39 for f in afs:
40 # abc.abc --> ['abc', 'abc'][-1] --> abc==abc.abc --> False
41 # abc --> ['abc'][-1] --> abc==abc --> True
42 d = f.split(os.extsep)[-1]
43 h = ''
44 if d != f:
45 h = '*.%s' %(d)
46 if h not in hs:
47 hs.append(h)
48 # *.txt-->{1..5}.txt *.png-->1.png
49 ts = []
50 for h in hs:
51 e = h[1:]
52 t = []
53 for f in afs:
54 if e == '':
55 if len(f.split(os.extsep)) == 1:
56 t.append(f)
57 elif f.endswith(e):
58 t.append(f)
59 ts.append(t)
60 r, c = 0, len(hs)
61 for t in ts:
62 lt = len(t)
63 if r < lt:
64 r = lt
65 for t in ts:
66 for i in range(r-len(t)):
67 t.append('')
68 for i in range(r):
69 b = []
70 for j in range(c):
71 b.append(ts[j][i])
72 bs.append(b)
73 ts = {'head': hs, 'body': bs}
74 s = 'class:%d file:%d' %(c, len(afs))
75 p = os.path.abspath(path)
76 rs = p + os.linesep + table.out_table(ts) + s + os.linesep
77 return rs
78
79 # test/abc
80 # test/1.txt
81 # test/abc/xyz
82 # test/abc/xyz/1.py
83 def get_directory(path):
84 ds = [os.path.abspath(path)]
85 fs = os.listdir(path)
86 for f in fs:
87 pf = os.path.join(path, f)
88 if not os.path.isdir(pf):
89 continue
90 ds.extend(get_directory(pf))
91 return ds
92
93 def get_mutiple_table(path):
94 ds = get_directory(path)
95 rs = ''
96 for d in ds:
97 rs += get_table(d)
98 return rs
99
100 def is_valid(path):
101 if not os.path.exists(path):
102 return False
103 if not os.path.isdir(path):
104 return False
105 return True
106
107 if __name__ == '__main__':
108 args = sys.argv[1:]
109 if '-h' in args or '--help' in args:
110 h = get_help()
111 print(h)
112 elif '-a' in args or '--author' in args:
113 a = get_author()
114 print(a)
115 elif '-v' in args or '--version' in args:
116 v = get_version()
117 print(v)
118 elif '-c' in args or '--copyright' in args:
119 c = get_copyright()
120 print(c)
121 elif '-m' in args or '--mutiple' in args:
122 for arg in args:
123 if not is_valid(arg):
124 continue
125 m = get_mutiple_table(arg)
126 print(m, end='')
127 else:
128 for arg in args:
129 if not is_valid(arg):
130 continue
131 t = get_table(arg)
132 print(t, end='')
创建一些测试文件:
mkdir test
touch test/{1..9}.jpg
touch test/{a..z}.doc
mkdir -p test/abc
mkdir -p test/xyz
mkdir -p test/xyz/none
touch test/abc/{1..3}.py
touch test/xyz/1.txt
执行结果:
![](https://images2018.cnblogs.com/blog/1276949/201805/1276949-20180527161237019-1320880899.png)
或:
![](https://images2018.cnblogs.com/blog/1276949/201805/1276949-20180527161310328-1770173119.png)
注意:目只支持文件名比较短的格式,有待进一步修改,谢谢!