Python_检查文件名是否规范(修改版)
来源:互联网 发布:seo超级伪静态 编辑:程序博客网 时间:2024/06/04 20:42
# coding=utf-8import osimport os.pathimport reimport arrayimport cmdimport pdbimport pickleimport tempfileimport subprocess# rootPath = os.getcwd()# print rootPathrootPath = 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-8import osimport os.pathimport reimport arrayimport cmdimport pdbimport pickleimport tempfileimport subprocessimport sysimport getoptrootPath = ""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-8import osimport os.pathimport reimport arrayimport cmdimport pdbimport pickleimport tempfileimport subprocessimport sysimport getoptnonCheckDirList = 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"
阅读全文
0 0
- Python_检查文件名是否规范(修改版)
- Python_修改文件名的一部分
- python小工具___检查文件名是否规范
- 检查文件名是否有效
- Python_修改渠道包的文件名
- 检查扩展名是否符合规范
- svn检查文件名中是否有空格
- 检查一个字符串是否是合法的windows文件名
- svn检查本地文件是否有改动,并显示文件名
- VBA 检查文件或文件名是否存在
- python_修改配置文件参数
- 修改 数据库文件名(逻辑文件名)
- MFC中检查CString类型的密码是否符合规范
- 检查CString类型的密码是否符合规范
- jupyter修改文件名(TensorFlow)
- 获取文件扩展名、 文件名、文件名不包括扩展名,检测文件夹、文件是否存在、创建文件夹,检测文件夹状态、检查文件名是否符合要求
- python_通过shelve修改对象
- 修改文件名
- HDU
- 直接选择排序
- POJ1789 解题报告
- JS函数 ,变量
- js 函数
- Python_检查文件名是否规范(修改版)
- K-Means(K均值聚类算法)
- Hogan模板
- Qt 写 XML
- 如何下载网页上不能下载的视频
- POJ 3984迷宫问题 (BFS做法)
- 使用浏览器端口打开HTML
- Variable 变量
- C++之条件语句,switch语句