您的位置:首页 > 编程语言 > Python开发

python 中文正则表达匹配

2014-05-11 22:15 218 查看
需求:由于某个n年前的工具的错误,在复制一批文件的时候产生了大量的"复件xxxxxxx""复件(2)XXXXX"等类似文件,由于目录结构深,文件多,预计在5000万个,但是有多少这种错误的文件不清楚,因此写个脚本遍历删除。

#encoding=utf-8
#author: skybug
#date: 2014-05-11
#function: 遍历指目录,删除中文开头的文件名的图片
import os,re
cnt = 0
pattern = re.compile(ur"[\u4e00-\u9fa5].*")#定义正则匹配表达式
#pattern = re.compile(ur"[\u590D][u4EF6].*")
def walkdir_del(srcdir):#遍历目录
global cnt
for parent,dirs,files in os.walk(srcdir):
for file in files:
infile = os.path.join(parent,file)
file = file.decode('gb2312').encode('utf8')#将文件名字符串转码
file = unicode(file,'utf8')#同上
match = pattern.match(file)#匹配
if match:#如果匹配
print infile
os.remove(infile)#删除
cnt +=1
print "del %s ok!"%infile
print "del %d files"%cnt

srcdir=os.getcwd()
walkdir_del(srcdir)


本文出自 “skybug” 博客,请务必保留此出处http://skybug.blog.51cto.com/132577/1409589
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: