如下所示:
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# coding=utf-8 import os import os.path import re import array import cmd import pdb import pickle import tempfile import subprocess # rootpath = os.getcwd() # print rootpath rootpath = raw_input ( 'the check path:' ) noncheckdir = raw_input ( 'the non check dirname(dirname1;dirname2):' ) noncheckdirlist = [] if noncheckdir: noncheckdirlist = noncheckdir.split( ';' ) # 路径字典 pathdic = {} # 新建文件夹 os.path.isdir(rootdir+'/logout') 判断指定目录下该文件夹是否存在 if not os.path.isdir(rootpath + '/logout' ): os.makedirs(rootpath + '/logout' ) logpath = os.path.join(rootpath, 'logout' ) nonstandard_filename_path = open (logpath + '/nonstandard_filename_path.txt' , 'w' ) # 标准的符号库 num = "0123456789" word = "abcdefghijklmnopqrstuvwxyz" sym = "_." # 符号库 symbank = [] for key in word: symbank.append(key) for key in num: symbank.append(key) for key in sym: symbank.append(key) def getalldir(getpath): # print (getpath) paths = os.listdir(getpath) for dirname in paths: dirpath = os.path.join(getpath,dirname) if os.path.isdir(dirpath) and dirname ! = '.svn' : # print dirpath relpath = dirpath[ len (rootpath) + 1 : len (dirpath)] # print relpath if not noncheckdirlist.__contains__(relpath): pathdic[relpath] = dirpath getalldir(dirpath) def getallfile(getpath): files = os.listdir(getpath) for filename in files: filepath = os.path.join(getpath,filename) if filename.endswith( '.png' ) or filename.endswith( '.png' ): filename = filename[ 0 :filename.index( '.' )] if not set (filename).issubset(symbank): # print filename # print filepath nonstandard_filename_path.write(filepath + '\n' ) else : # (r'_[\d]*[x|x][\d]*\d') 正则表达式 (_100x100) sign = re.search(r '_[\d]*[x|x][\d]*\d' ,filename,re.m|re.i) if sign: nonstandard_filename_path.write(filepath + '\n' ) if __name__ = = '__main__' : print ( 'main' ) pathdic[ 'curpath' ] = rootpath getalldir(rootpath) for key in pathdic: # print key getallfile(pathdic[key]) # line = "image_500100000" # obj = re.search(r'_[\d]*[x|x][\d]*\d',line,re.m|re.i) # line = line.replace(obj.group(),'=') # if obj: # print obj.group() # else: # print ("==-") # line1 = "image_500x100" # obj1 = re.search(r'[a-z0-9_]*',line1,re.m) # print obj1.group() |
新建bat后缀文件
1
2
|
find_nonstandard_name.exe - c @pause |
修改后脚本
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
|
# coding=utf-8 import os import os.path import re import array import cmd import pdb import pickle import tempfile import subprocess import sys import getopt rootpath = "" noncheckdirlist = sys.argv[ 1 :] opts, args = getopt.getopt(sys.argv[ 1 :], "cs:" ,[ "cpath=" ]) for opt,arg in opts: if opt = = '-c' : rootpath = os.getcwd() elif opt in ( "-s" , "--cpath" ): rootpath = arg # 路径字典 pathdic = {} # 新建文件夹 os.path.isdir(rootdir+'/logout') 判断指定目录下该文件夹是否存在 if not os.path.isdir(rootpath + '/logout' ): os.makedirs(rootpath + '/logout' ) logpath = os.path.join(rootpath, 'logout' ) nonstandard_filename_path = open (logpath + '/nonstandard_filename_path.txt' , 'w' ) def getalldir(getpath): # print (getpath) paths = os.listdir(getpath) for dirname in paths: dirpath = os.path.join(getpath,dirname) if os.path.isdir(dirpath) and dirname ! = '.svn' : # print dirpath relpath = dirpath[ len (rootpath) + 1 : len (dirpath)] # print relpath if not noncheckdirlist.__contains__(relpath): pathdic[relpath] = dirpath getalldir(dirpath) def getallfile(getpath): files = os.listdir(getpath) for filename in files: filepath = os.path.join(getpath,filename) if filename.endswith( '.png' ) or filename.endswith( '.png' ): filename = filename[ 0 :filename.index( '.' )] firstsign = re.search(r '^[a-z0-9_]*$' ,line1,re.m) if firstsign: # print filepath # (r'_[\d]*[x|x][\d]*\d') 正则表达式 (_100x100) sign = re.search(r '_[\d]*[x|x][\d]*\d' , filename, re.m | re.i) if sign: print filename nonstandard_filename_path.write(filepath + '\n' ) else : print filename nonstandard_filename_path.write(filepath + '\n' ) if __name__ = = '__main__' : print ( 'main' ) pathdic[ 'curpath' ] = rootpath getalldir(rootpath) for key in pathdic: # print key getallfile(pathdic[key]) |
添加检查文件重名功能
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# coding=utf-8 import os import os.path import re import array import cmd import pdb import pickle import tempfile import subprocess import sys import getopt noncheckdirlist = sys.argv[ 1 :] rootpath = os.getcwd() checkrepetpathlist = [] if noncheckdirlist: rootpath = os.path.realpath(os.path.join(os.getcwd(),noncheckdirlist[ 0 ])) if noncheckdirlist[ 0 ] = = "./" : rootpath = os.getcwd() for _path in noncheckdirlist: # -- 检查重命名路径 _cmdrepet = _path[ 0 : 2 ] if _cmdrepet = = "/r" : repetpath = _path[ len (_cmdrepet): len (_path)] print repetpath checkrepetpathlist.append(repetpath) print rootpath + '\n' # 路径字典 pathdic = {} # 重名路径字典 repetdic = {} # 新建文件夹 os.path.isdir(rootdir+'/logout') 判断指定目录下该文件夹是否存在 # if not os.path.isdir(rootpath+'/logout'): # os.makedirs(rootpath + '/logout') # logpath=os.path.join(rootpath,'logout') logpath = os.getcwd() nonstandard_filename_path = open (logpath + "\\"+u" 不规范命名文件 ".encode(" gbk ") + " .txt", 'w' ) def getalldir(getpath): # print (getpath) paths = os.listdir(getpath) for dirname in paths: dirpath = os.path.join(getpath,dirname) if os.path.isdir(dirpath) and dirname ! = '.svn' : # print dirpath relpath = dirpath[ len (rootpath) + 1 : len (dirpath)] # print relpath if not noncheckdirlist.__contains__(relpath): pathdic[relpath] = dirpath getalldir(dirpath) def getallfile(getpath): files = os.listdir(getpath) for filename in files: filepath = os.path.join(getpath,filename) if filename.endswith( '.png' ) or filename.endswith( '.png' ): filename = filename[ 0 :filename.index( '.' )] firstsign = re.search(r '^[a-z0-9_]*$' ,filename,re.m) if firstsign: # print filepath # (r'_[\d]*[x|x][\d]*\d') 正则表达式 (_100x100) sign = re.search(r '_[\d]*[x|x][\d]*\d' , filename, re.m | re.i) if sign: print filename nonstandard_filename_path.write(filepath + '\n' ) else : print filename nonstandard_filename_path.write(filepath + '\n' ) def checkrepetfile(getpath): if checkrepetpathlist: paths = os.listdir(getpath) for dirname in paths: dirpath = os.path.join(getpath, dirname) if os.path.isdir(dirpath) and dirname ! = '.svn' : # print dirpath relpath = dirpath[ len (rootpath) + 1 : len (dirpath)] # print relpath repetdic[relpath] = dirpath checkrepetfile(dirpath) imagelist = [] repetimagepath = [] def getcheckrepetfile(getpath): files = os.listdir(getpath) for filename in files: filepath = os.path.join(getpath, filename) if filename.endswith( '.png' ) or filename.endswith( '.png' ): # print filepath imagelist.append(filename) repetimagepath.append(filepath) repet_filename_path = open (logpath + "\\"+u" 重复命名文件 ".encode(" gbk ") + " .txt", 'w' ) if __name__ = = '__main__' : # print ('main') pathdic[ 'curpath' ] = rootpath getalldir(rootpath) for key in pathdic: # print key getallfile(pathdic[key]) print '\n' + "the logout path:" + logpath + "\\"+u" 不规范命名文件 ".encode(" gbk ") + " .txt" repetdic[ 'curpath' ] = rootpath # 检查重复文件路径列表 for __path in checkrepetpathlist: _repetpath = os.path.join(rootpath, __path) checkrepetfile(_repetpath) # 遍历路径获得所有图片 for key in repetdic: getcheckrepetfile(repetdic[key]) _newimagelist = [] for image in imagelist: repetcount = imagelist.count(image) if repetcount > 1 : if not image in _newimagelist: _newimagelist.append(image) for repetimage in _newimagelist: print repetimage repet_filename_path.write(repetimage + '\n' ) for repetpathpath in repetimagepath: filenamename = os.path.basename(repetpathpath) if repetimage = = filenamename: repet_filename_path.write(repetpathpath + '\n' ) # print repetpathpath print '\n' + "the logout path:" + logpath + "\\"+u" 重复命名文件 ".encode(" gbk ") + " .txt" |
以上这篇对python 检查文件名是否规范的实例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/sinat_36373961/article/details/76559513