码迷,mamicode.com
首页 > 编程语言 > 详细

Match files that match pattern in Groovy and Python

时间:2016-11-01 19:32:05      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:import   app   groovy   methods   sys   nes   port   delete   join   

转自: 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

标签:import   app   groovy   methods   sys   nes   port   delete   join   

原文地址:http://www.cnblogs.com/z1500592/p/6020466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!