对Python 检查文件名是否规范的实例详解


Posted in Python onJune 10, 2019

如下所示:

# 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 相关文章推荐
Python3基础之函数用法
Aug 13 Python
Python快速从注释生成文档的方法
Dec 26 Python
Django日志模块logging的配置详解
Feb 14 Python
python中获得当前目录和上级目录的实现方法
Oct 12 Python
Python中property属性实例解析
Feb 10 Python
Python网络编程之TCP套接字简单用法示例
Apr 09 Python
利用Python实现在同一网络中的本地文件共享方法
Jun 04 Python
Windows 64位下python3安装nltk模块
Sep 19 Python
使用python实现CGI环境搭建过程解析
Apr 28 Python
解决python调用自己文件函数/执行函数找不到包问题
Jun 01 Python
python 实现Requests发送带cookies的请求
Feb 08 Python
Python之qq自动发消息的示例代码
Feb 18 Python
java判断三位数的实例讲解
Jun 10 #Python
Python字符串的一些操作方法总结
Jun 10 #Python
利用selenium爬虫抓取数据的基础教程
Jun 10 #Python
Python 监测文件是否更新的方法
Jun 10 #Python
python实现随机漫步方法和原理
Jun 10 #Python
使用python判断jpeg图片的完整性实例
Jun 10 #Python
关于Python作用域自学总结
Jun 10 #Python
You might like
PHP4.04简明安装
2006/10/09 PHP
PHP 金额数字转换成英文
2010/05/06 PHP
Centos7安装swoole扩展操作示例
2020/03/26 PHP
javascript中实现兼容JAVA的hashCode算法代码分享
2020/08/11 Javascript
JavaScript编写一个简易购物车功能
2016/09/17 Javascript
微信小程序 自动登陆PHP源码实例(源码下载)
2017/05/08 Javascript
angular4自定义组件详解
2017/09/28 Javascript
React.js绑定this的5种方法(小结)
2018/06/05 Javascript
微信小程序定位当前城市的方法
2018/07/19 Javascript
微信小程序swiper左右扩展各显示一半代码实例
2019/12/05 Javascript
JS removeAttribute()方法实现删除元素的某个属性
2021/01/11 Javascript
Python编程实现从字典中提取子集的方法分析
2018/02/09 Python
Python实现读写INI配置文件的方法示例
2018/06/09 Python
python标记语句块使用方法总结
2019/08/05 Python
Python3 使用pillow库生成随机验证码
2019/08/26 Python
tensorflow estimator 使用hook实现finetune方式
2020/01/21 Python
python 装饰器的实际作用有哪些
2020/09/07 Python
python实现快速文件格式批量转换的方法
2020/10/16 Python
CSS3弹性伸缩布局之box布局
2016/07/12 HTML / CSS
Petmate品牌官方网站:宠物用品
2018/11/25 全球购物
PHP如何对用户密码进行加密
2014/07/31 面试题
PHP如何删除一个Cookie值
2012/11/15 面试题
中学教师实习自我鉴定
2013/09/28 职场文书
英语专业学生个人求职信
2014/01/28 职场文书
婚礼主持词
2014/03/13 职场文书
房屋买卖协议书范本
2014/04/10 职场文书
运动会拉拉队口号
2014/06/09 职场文书
煤矿安全知识竞赛活动总结
2014/07/07 职场文书
四风问题个人自查剖析材料思想汇报
2014/09/21 职场文书
寝室长工作失责检讨书
2014/10/06 职场文书
党员群众路线剖析材料
2014/10/08 职场文书
幼儿教师2014年度工作总结
2014/12/16 职场文书
好好学习保证书
2015/02/26 职场文书
2015年财务人员工作总结
2015/04/10 职场文书
幼师辞职信范文大全
2015/05/12 职场文书
公司人事管理制度
2015/08/05 职场文书