Detailed examples of Python checking file names for specifications
- 2021-06-28 09:28:49
- OfStack
As follows:
# 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(';')
# Path Dictionary
pathDic = {}
# New Folder os.path.isdir(rootdir+'/logout') Determine if the folder exists in the specified directory
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')
# Standard Symbol Library
num = "0123456789"
word = "abcdefghijklmnopqrstuvwxyz"
sym = "_."
# Symbol Library
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') regular expression ( _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()
New bat Suffix File
find_nonstandard_name.exe -c
@pause
Modified script
# 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
# Path Dictionary
pathDic = {}
# New Folder os.path.isdir(rootdir+'/logout') Determine if the folder exists in the specified directory
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') regular expression ( _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])
Add check file rename function
# 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:
# -- Check Renamed Path
_cmdRepet = _path[0:2]
if _cmdRepet == "/r":
repetPath = _path[len(_cmdRepet):len(_path)]
print repetPath
checkRepetPathList.append(repetPath)
print rootPath + '\n'
# Path Dictionary
pathDic = {}
# Duplicate Name Path Dictionary
repetDic = {}
# New Folder os.path.isdir(rootdir+'/logout') Determine if the folder exists in the specified directory
# 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" Nonstandard named file ".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') regular expression ( _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" Duplicate Naming of Files ".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" Nonstandard named file ".encode("GBK") + ".txt"
repetDic['curPath'] = rootPath
# Check list of duplicate file paths
for __path in checkRepetPathList:
_repetPath = os.path.join(rootPath, __path)
CheckRepetFile(_repetPath)
# Traverse the path to get all pictures
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" Duplicate Naming of Files ".encode("GBK") + ".txt"