欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-12 20:25:16
如下所示: # coding=utf-8 import os import os.path import re import array import...

如下所示:

# 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 检查文件名是否规范的实例详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。