python 中文正则表达匹配
2014-05-11 22:15
218 查看
需求:由于某个n年前的工具的错误,在复制一批文件的时候产生了大量的"复件xxxxxxx""复件(2)XXXXX"等类似文件,由于目录结构深,文件多,预计在5000万个,但是有多少这种错误的文件不清楚,因此写个脚本遍历删除。
本文出自 “skybug” 博客,请务必保留此出处http://skybug.blog.51cto.com/132577/1409589
#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
相关文章推荐
- Python 3 正则表达式对中文的匹配
- Python正则表达式匹配中文
- Python正则表达式匹配中文
- Python中正则表达式对中文的匹配问题
- Python网络爬虫中的网页中文正则表达式匹配小心得
- Python中正则表达式对中文的匹配问题
- python正则匹配中文字符(包括冒号…
- python 正则表达式匹配中文(转)
- Python从文件中读取字符串,用正则表达式匹配中文字符的问题
- Python中正则表达式对中文的匹配问题
- 用python3.x正则表达式匹配中文字符串
- Python 语言及其应用 Chapter_7_Note 1 正则表达匹配
- python正则表达式匹配中文
- Python正则表达式处理中文中的匹配
- Python中正则表达式对中文的匹配问题
- python中文正则匹配初步使用
- Python中使用中文正则表达式匹配指定的中文字符串
- python正则表达式匹配中文
- Python2.7 转义和正则匹配中文
- Python正则匹配中文与编码总结