对Python 检查文件名是否规范的实例详解
作者:愚公丨Acmen 发布时间:2021-11-13 02:04:23
标签:Python,文件名,规范
如下所示:
# 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"
来源:https://blog.csdn.net/sinat_36373961/article/details/76559513


猜你喜欢
- 一、前言B站上的漂亮的小姐姐真的好多好多,利用 you-get * 下载了一个 B 站上跳舞的小姐姐视频,利用视频中的弹幕来制作一个漂亮小姐
- 和YSlow一样,“Page Speed”也是一个基于firebug附加组件的FireFox插件。虽然听起来有点拗口,但是意思很容易理解:如
- 这篇文章主要介绍了Python和Sublime整合过程图示,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- Python在很大程度上可以对shell脚本进行替代。笔者一般单行命令用shell,复杂点的多行操作就直接用Python了。这篇文章就归纳一
- declare @id  
- 俗话说,“工欲善其事,必先利其器”。对于前端开发工程师来说,基于Firefox丰富的Web开发辅助插件无疑就是最好的利器。下面就与大家分享2
- 首先是安装好PHP之后配置环境变量然后在cmd中输入php -v 能看到版本号即为配置好了之后在sublime中新建编译系统,输入代码{&q
- 前言本文主要是积累一下在使用前端的watch开发过程中遇到的问题点和经验。一、watch是什么根据本人的理解,它就是一个 * ,就是说监听的
- 一、什么是七段数码显示器 七段LCD数码显示器
- 1.global关键字默认情况下,在局部作用域对全局变量只能进行:读取,修改内部元素(可变类型),无法对全局变量进行重新赋值读取:CITY=
- 该章节我们将要学习如何将 word 文件转为 PDF文件,其实网上有很多种生成 PDF 的教程,不过绝大多数都是以 windows 为主的,
- 目录OpenCV先决条件我们会在本文中涵盖7个主题读,写和显示图像imread():imshow():imwrite():读取视频并与网络摄
- MySQL中涉及的几个字符集 character-set-server/default-character-set:服务器字符集,默认情况下
- Nonetype和空值是不一致的,可以理解为Nonetype为不存在这个参数,空值表示参数存在,但是值为空判断方式如下:if hostip
- 在 MySQL 主从复制时,有时候会碰到这样的故障:在 Slave 上 Slave_IO_Running 和 Slave_SQL_Runni
- 小编语:小编我最早使用的网页制作工作就是FrontPage啦,FrongPage方便简单,会用Word就能做网页。如果你对网页制作要求不高,
- 首先简单介绍一下通配符,用来匹配值的一部分的特殊字符。搜索模式(search pattern) 由字面值、通配符或两者组合构成的
- 1.event.srcElement //srcElement只能在IE下使用target是FireFox使用的,下面是兼容性写法 var
- 有两张表a表id val 1 a 2 b 3 c 4 d 5 e b表 a_id val 1 null 2 null 3 null 4 nu
- 前言Python文件默认的编码格式是ascii ,无法识别汉字,因为ascii码中没有中文。所以py文件中要写中文字符时,一般在开头加 #