天天看点

python文件管理器_python——linux文件分类管理器

cat zero.py——调用table表模板,然后制作文件分类管理器

1 #!/usr/bin/env python

2 #-*- coding: utf-8 -*-

3 importtable4 importsys5 importos, os.path6

7 defget_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] show all files of class in sepcified PATH.'

13 returnrs14

15 defget_copyright():16 rs = '(c)2011-2018 [email protected] all rights reserved'

17 returnrs18

19 defget_author():20 rs = 'Author: kevin'

21 returnrs22

23 defget_version():24 return '1.0.0'

25

26 defget_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 infs:36 ifos.path.isdir(os.path.join(path, f)):37 continue

38 afs.append(f)39 for f inafs: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 inhs:47 hs.append(h)48 #*.txt-->{1..5}.txt *.png-->1.png

49 ts =[]50 for h inhs:51 e = h[1:]52 t =[]53 for f inafs:54 if e == '':55 if len(f.split(os.extsep)) == 1:56 t.append(f)57 eliff.endswith(e):58 t.append(f)59 ts.append(t)60 r, c =0, len(hs)61 for t ints:62 lt =len(t)63 if r <64 r="lt65" for t ints:66 i in range t.append inrange b="[]70" j b.append bs.append ts="{'head':" hs bs s="class:%d file:%d" len p="os.path.abspath(path)76" rs="p" os.linesep table.out_table returnrs78>

79 #test/abc

80 #test/1.txt

81 #test/abc/xyz

82 #test/abc/xyz/1.py

83 defget_directory(path):84 ds =[os.path.abspath(path)]85 fs =os.listdir(path)86 for f infs:87 pf =os.path.join(path, f)88 if notos.path.isdir(pf):89 continue

90 ds.extend(get_directory(pf))91 returnds92

93 defget_mutiple_table(path):94 ds =get_directory(path)95 rs = ''

96 for d inds:97 rs +=get_table(d)98 returnrs99

100 defis_valid(path):101 if notos.path.exists(path):102 returnFalse103 if notos.path.isdir(path):104 returnFalse105 returnTrue106

107 if __name__ == '__main__':108 args = sys.argv[1:]109 if '-h' in args or '--help' inargs:110 h =get_help()111 print(h)112 elif '-a' in args or '--author' inargs:113 a =get_author()114 print(a)115 elif '-v' in args or '--version' inargs:116 v =get_version()117 print(v)118 elif '-c' in args or '--copyright' inargs:119 c =get_copyright()120 print(c)121 elif '-m' in args or '--mutiple' inargs:122 for arg inargs:123 if notis_valid(arg):124 continue

125 m =get_mutiple_table(arg)126 print(m, end='')127 else:128 for arg inargs:129 if notis_valid(arg):130 continue

131 t =get_table(arg)132 print(t, end='')

64>