对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
0
投稿
猜你喜欢
- 作为一个网页设计师,不知道各位是否有这样的经历:客户给你的网站材料很多都是Word文档,虽然阅读起来很
- 上一篇介绍了 HTML5 中 Canvas 的基本概念,这篇将要介绍一下 Canvas&n
- map( )函数在算法题目里面经常出现,map( )会根据提供的函数对指定序列做映射,在写返回值等需要转换的时候比较常用。关于映射map,可
- // 格式化字符串 Fmt("{0}.[{id}].{name}",{id:1,name:'
- 本文实例讲述了python实现生成Word、docx文件的方法。分享给大家供大家参考,具体如下:http://python-docx.rea
- 在pycharm使用过程中,对于每次新建的python文件的时候,关于代码编写者的一些个人信息快捷填写,使用模板的方式比较方便。方法如下:1
- 代码如下:Set Catalog_object= Server.CreateObject("ADO
- Pygame的Event事件模块事件(Event)是 Pygame 的重要模块之一,它是构建整个游戏程序的核心,比如常用的鼠标点击、键盘敲击
- 当Anconda安装多个python环境时,或者由于多次卸载安装Anaconda时,导致python的kernel内核启动失败问题,使得ju
- subprocess官方中文文档介绍参考文档,我的直观感受和实际用法是:subprocess可以开启一个子进程来运行cmd命令。那就意味着可
- PL/SQL是ORACLE对标准数据库语言的扩展,ORACLE公司已经将PL/SQL整合到ORACLE 服务器和其他工具中了,近几年中更多的
- pyfinance简介在查找如何使用Python实现滚动回归时,发现一个很有用的量化金融包——pyfinance。顾名思义,pyfinanc
- flask-login 模块简介Flask-Login 是一个 Flask 模块,可以为 Flask 应用程序提供用户登录功能。要在 Fla
- 依赖环境本篇文章所依赖的python 环境如下:计算机网络应用体系结构,一般有以下几种:C/S架构: 也称为客户端/客户端架构。P2P架构:
- 前言大家都知道,英文的分词由于单词间是以空格进行分隔的,所以分词要相对的容易些,而中文就不同了,中文中一个句子的分隔就是以字为单位的了,而所
- 一、读写文件在 Python 中,我们可以使用 open() 函数打开文件,with 语句可以自动关闭文件。读取文件内容:with open
- 在程序运行的过程中,所有的变量都是在内存中,比如,定义一个dict:d = dict(name='Bob', age=20,
- 模块是用类编写的,只有一个StringIO类,所以它的可用方法都在类中。此类中的大部分函数都与对文件的操作方法类似。例:#coding=gb
- 代码如下:<% function GetBot() '查询蜘蛛 dim s_
- 在了解装饰器的之前一定要先了解函数作为参数传递, 什么是函数内嵌,请参考我之前写的博客函数简介因为在python里面,函数也是对象,也可以作