findfile.py
1.19 KB
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
import os
import fnmatch
def find_pattern_all(pattern, path, casesensitive):
result = []
for root, dirs, files in os.walk(path):
for name in files:
if casesensitive:
if fnmatch.fnmatchcase(name,pattern):
result.append(os.path.join(root, name))
else:
if fnmatch.fnmatch(name.lower(), pattern.lower()):
result.append(os.path.join(root, name))
return result
def find_pattern(pattern, path, casesensitive):
for root, dirs, files in os.walk(path):
for name in files:
if casesensitive:
if fnmatch.fnmatchcase(name,pattern):
return os.path.join(root, name)
else:
if fnmatch.fnmatch(name.lower(), pattern.lower()):
return os.path.join(root, name)
return None
def find_all(name, path):
result = []
for root, dirs, files in os.walk(path):
if name in files:
result.append(os.path.join(root, name))
return result
def find(name, path):
for root, dirs, files in os.walk(path):
if name in files:
return os.path.join(root, name)
return None