File: recursiveglob.py

package info (click to toggle)
activiz.net 1%3A1.0~git20111214-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,280 kB
  • ctags: 5,957
  • sloc: cs: 28,767; python: 915; cpp: 130; makefile: 35; sh: 11
file content (75 lines) | stat: -rw-r--r-- 1,541 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
##
## Recurive glob example
## 9/29/2006
## hosted by: Berlin Brown
##
## jython -c "import blb.DirAll as b;b.main()" -v
##

import os
import glob
import sys
import fnmatch

#
# Globals 
verboseflag = 0
pathaccess_ctr = 0

def glob_get_dirs(path):
    ''' Return a list of directorie at this particular path '''
    global verboseflag
    global pathaccess_ctr

    d = []
    if verboseflag:
        print "  scanning=*", path
    else:
        pathaccess_ctr = pathaccess_ctr + 1
        #print ".",
        if pathaccess_ctr == 40:
            print

    try:
        for i in os.listdir(path):          
            if os.path.isdir(path+i):
                d.append(os.path.basename(i))

    except NameError, ne:
        print "NameError thrown=", ne
    except:
        print sys.exc_info()[0]
        print "ERR in get_dirs()"

    return d

def rec_glob(path, mask):
    ''' Recursive glob on the following path '''
    l = []
    if path[-1] != '\\':
        path = path + '\\'

    for i in glob_get_dirs(path):
        res = rec_glob(path + i, mask)
        l = l + res

    try:
        for i in os.listdir(path):
            ii = i
            i = path + i
            if os.path.isfile(i):
                if fnmatch.fnmatch(ii, mask):
                    l.append(i)
                    if verboseflag:
                        print "file=", i

    except NameError, ne:
        print "NameError thrown=", ne
    except:
        print sys.exc_info()[0]
        print "ERR in rec_glob()"

    return l    

# End of the File