首页 > 代码库 > Match files that match pattern in Groovy and Python
Match files that match pattern in Groovy and Python
转自: http://atobs.blogspot.fr/2012/08/match-only-files-that-match-pattern-in.html#!/2012/08/match-only-files-that-match-pattern-in.html
Groovy:
In Groovy we can use the eachDirRecurse and eachFileMatch() methods to get all the file names displayed.
def pattern = ~/.*\.java/
def dirname="/usr/local/mysource"
new File("$dirname").eachDirRecurse { dir ->
dir.eachFileMatch(pattern) { myfile ->
println "$myfile"
} // eachFileMatch
} // eachFileMatch
Python:
In Python, we can list each matching file using "glob"
import os, glob, sys
for root, dirs, files in os.walk( ‘E:\\users‘ ):
os.chdir (root)
# find all files that match log*
logs = glob.glob( ‘*log*‘ )
if logs:
for fname in logs:
fullpath = os.path.join ( root, fname )
# Identify files of len 3 lines long and delete them
count = len ( open(fullpath).readlines() )
if count == 3 or count == 2:
print ‘Removing ‘, fullpath
os.remove ( fullpath )
Match files that match pattern in Groovy and Python
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。