对Python 检查文件名是否规范的实例详解
如下所示:
#coding=utf-8 importos importos.path importre importarray importcmd importpdb importpickle importtempfile importsubprocess #rootPath=os.getcwd() #printrootPath rootPath=raw_input('TheCheckPath:') nonCheckDir=raw_input('TheNonCheckDirName(DirName1;DirName2):') nonCheckDirList=[] ifnonCheckDir: nonCheckDirList=nonCheckDir.split(';') #路径字典 pathDic={} #新建文件夹os.path.isdir(rootdir+'/logout')判断指定目录下该文件夹是否存在 ifnotos.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=[] forkeyinword: symBank.append(key) forkeyinnum: symBank.append(key) forkeyinsym: symBank.append(key) defGetAllDir(getPath): #print(getPath) paths=os.listdir(getPath) fordirNameinpaths: dirPath=os.path.join(getPath,dirName) ifos.path.isdir(dirPath)anddirName!='.svn': #printdirPath relPath=dirPath[len(rootPath)+1:len(dirPath)] #printrelPath ifnotnonCheckDirList.__contains__(relPath): pathDic[relPath]=dirPath GetAllDir(dirPath) defGetAllFile(getPath): files=os.listdir(getPath) forfileNameinfiles: filePath=os.path.join(getPath,fileName) iffileName.endswith('.png')orfileName.endswith('.PNG'): fileName=fileName[0:fileName.index('.')] ifnotset(fileName).issubset(symBank): #printfileName #printfilePath 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) ifsign: nonstandard_filename_path.write(filePath+'\n') if__name__=='__main__': print('main') pathDic['curPath']=rootPath GetAllDir(rootPath) forkeyinpathDic: #printkey 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(),'=') #ifobj: #printobj.group() #else: #print("==-") #line1="image_500x100" #obj1=re.search(r'[a-z0-9_]*',line1,re.M) #printobj1.group()
新建bat后缀文件
find_nonstandard_name.exe-c @pause
修改后脚本
#coding=utf-8 importos importos.path importre importarray importcmd importpdb importpickle importtempfile importsubprocess importsys importgetopt rootPath="" nonCheckDirList=sys.argv[1:] opts,args=getopt.getopt(sys.argv[1:],"cs:",["cPath="]) foropt,arginopts: ifopt=='-c': rootPath=os.getcwd() elifoptin("-s","--cPath"): rootPath=arg #路径字典 pathDic={} #新建文件夹os.path.isdir(rootdir+'/logout')判断指定目录下该文件夹是否存在 ifnotos.path.isdir(rootPath+'/logout'): os.makedirs(rootPath+'/logout') logPath=os.path.join(rootPath,'logout') nonstandard_filename_path=open(logPath+'/nonstandard_filename_path.txt','w') defGetAllDir(getPath): #print(getPath) paths=os.listdir(getPath) fordirNameinpaths: dirPath=os.path.join(getPath,dirName) ifos.path.isdir(dirPath)anddirName!='.svn': #printdirPath relPath=dirPath[len(rootPath)+1:len(dirPath)] #printrelPath ifnotnonCheckDirList.__contains__(relPath): pathDic[relPath]=dirPath GetAllDir(dirPath) defGetAllFile(getPath): files=os.listdir(getPath) forfileNameinfiles: filePath=os.path.join(getPath,fileName) iffileName.endswith('.png')orfileName.endswith('.PNG'): fileName=fileName[0:fileName.index('.')] firstSign=re.search(r'^[a-z0-9_]*$',line1,re.M) iffirstSign: #printfilePath #(r'_[\d]*[x|X][\d]*\d')正则表达式(_100x100) sign=re.search(r'_[\d]*[x|X][\d]*\d',fileName,re.M|re.I) ifsign: printfileName nonstandard_filename_path.write(filePath+'\n') else: printfileName nonstandard_filename_path.write(filePath+'\n') if__name__=='__main__': print('main') pathDic['curPath']=rootPath GetAllDir(rootPath) forkeyinpathDic: #printkey GetAllFile(pathDic[key])
添加检查文件重名功能
#coding=utf-8 importos importos.path importre importarray importcmd importpdb importpickle importtempfile importsubprocess importsys importgetopt nonCheckDirList=sys.argv[1:] rootPath=os.getcwd() checkRepetPathList=[] ifnonCheckDirList: rootPath=os.path.realpath(os.path.join(os.getcwd(),nonCheckDirList[0])) ifnonCheckDirList[0]=="./": rootPath=os.getcwd() for_pathinnonCheckDirList: #--检查重命名路径 _cmdRepet=_path[0:2] if_cmdRepet=="/r": repetPath=_path[len(_cmdRepet):len(_path)] printrepetPath checkRepetPathList.append(repetPath) printrootPath+'\n' #路径字典 pathDic={} #重名路径字典 repetDic={} #新建文件夹os.path.isdir(rootdir+'/logout')判断指定目录下该文件夹是否存在 #ifnotos.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') defGetAllDir(getPath): #print(getPath) paths=os.listdir(getPath) fordirNameinpaths: dirPath=os.path.join(getPath,dirName) ifos.path.isdir(dirPath)anddirName!='.svn': #printdirPath relPath=dirPath[len(rootPath)+1:len(dirPath)] #printrelPath ifnotnonCheckDirList.__contains__(relPath): pathDic[relPath]=dirPath GetAllDir(dirPath) defGetAllFile(getPath): files=os.listdir(getPath) forfileNameinfiles: filePath=os.path.join(getPath,fileName) iffileName.endswith('.png')orfileName.endswith('.PNG'): fileName=fileName[0:fileName.index('.')] firstSign=re.search(r'^[a-z0-9_]*$',fileName,re.M) iffirstSign: #printfilePath #(r'_[\d]*[x|X][\d]*\d')正则表达式(_100x100) sign=re.search(r'_[\d]*[x|X][\d]*\d',fileName,re.M|re.I) ifsign: printfileName nonstandard_filename_path.write(filePath+'\n') else: printfileName nonstandard_filename_path.write(filePath+'\n') defCheckRepetFile(getPath): ifcheckRepetPathList: paths=os.listdir(getPath) fordirNameinpaths: dirPath=os.path.join(getPath,dirName) ifos.path.isdir(dirPath)anddirName!='.svn': #printdirPath relPath=dirPath[len(rootPath)+1:len(dirPath)] #printrelPath repetDic[relPath]=dirPath CheckRepetFile(dirPath) imageList=[] repetImagePath=[] defGetCheckRepetFile(getPath): files=os.listdir(getPath) forfileNameinfiles: filePath=os.path.join(getPath,fileName) iffileName.endswith('.png')orfileName.endswith('.PNG'): #printfilePath 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) forkeyinpathDic: #printkey GetAllFile(pathDic[key]) print'\n'+"TheLogoutPath:"+logPath+"\\"+u"不规范命名文件".encode("GBK")+".txt" repetDic['curPath']=rootPath #检查重复文件路径列表 for__pathincheckRepetPathList: _repetPath=os.path.join(rootPath,__path) CheckRepetFile(_repetPath) #遍历路径获得所有图片 forkeyinrepetDic: GetCheckRepetFile(repetDic[key]) _newImageList=[] forimageinimageList: repetCount=imageList.count(image) ifrepetCount>1: ifnotimagein_newImageList: _newImageList.append(image) forrepetImagein_newImageList: printrepetImage repet_filename_path.write(repetImage+'\n') forrepetPathPathinrepetImagePath: fileNameName=os.path.basename(repetPathPath) ifrepetImage==fileNameName: repet_filename_path.write(repetPathPath+'\n') #printrepetPathPath print'\n'+"TheLogoutPath:"+logPath+"\\"+u"重复命名文件".encode("GBK")+".txt"
以上这篇对Python检查文件名是否规范的实例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。