圆月山庄资源网 Design By www.vgjia.com
如下所示:
# 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后缀文件
find_nonstandard_name.exe -c @pause
修改后脚本
# 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])
添加检查文件重名功能
# 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 检查文件名是否规范的实例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
标签:
Python,文件名,规范
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月06日
2024年11月06日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]