使用Python怎么檢查文件名是否規(guī)范-創(chuàng)新互聯(lián)

這篇文章給大家介紹使用Python怎么檢查文件名是否規(guī)范,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站制作、網(wǎng)站建設(shè)、網(wǎng)站頁面設(shè)計(jì)、網(wǎng)站優(yōu)化SEO優(yōu)化為一體的專業(yè)網(wǎng)站建設(shè)公司,已為成都等多地近百家企業(yè)提供網(wǎng)站建設(shè)服務(wù)。追求良好的瀏覽體驗(yàn),以探求精品塑造與理念升華,設(shè)計(jì)最適合用戶的網(wǎng)站頁面。 合作只是第一步,服務(wù)才是根本,我們始終堅(jiān)持講誠信,負(fù)責(zé)任的原則,為您進(jìn)行細(xì)心、貼心、認(rèn)真的服務(wù),與眾多客戶在蓬勃發(fā)展的市場(chǎng)環(huán)境中,互促共生。

如下所示:

# 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')
 
# 標(biāo)準(zhǔn)的符號(hào)庫
num = "0123456789"
word = "abcdefghijklmnopqrstuvwxyz"
sym = "_."
# 符號(hào)庫
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') 正則表達(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') 正則表達(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"不規(guī)范命名文件".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') 正則表達(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"重復(fù)命名文件".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"不規(guī)范命名文件".encode("GBK") + ".txt"
 
 
  repetDic['curPath'] = rootPath
  # 檢查重復(fù)文件路徑列表
  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"重復(fù)命名文件".encode("GBK") + ".txt"

關(guān)于使用Python怎么檢查文件名是否規(guī)范就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)頁題目:使用Python怎么檢查文件名是否規(guī)范-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://bm7419.com/article46/goihg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站制作、Google、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管