-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_pattern_in_directory_tree.py
56 lines (45 loc) · 1.33 KB
/
check_pattern_in_directory_tree.py
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
import os
import re
## initialize these variables
dir2=r"K:\customers\nakilath\ffo_upgrade_2019"
#dir2=r"K:\customers\emke\upgrade_project_2018\emke"
#dir2=r"C:\Users\ABUSHAHULS\Documents\temp"
regex2=r"ff_sid_adm.pl.*add"
##
print("directory name is", dir2)
filelist=[]
def get_files_in_a_directory(dir1):
global filelist
if os.path.isdir(dir1):
listlevel1=os.listdir(dir1)
if (len(listlevel1)==0):
#print("no files found in", dir1)
return
else:
for i in listlevel1:
if os.path.isfile(dir1+'\\'+i):
filelist.append(dir1+'\\'+i)
elif os.path.isdir(dir1+'\\'+i):
get_files_in_a_directory(dir1+'\\'+i)
else:
print(dir1,"is NOT a directory.")
## function 2
def check_pattern_in_file(file1, regex1):
fin = open(file1, 'rt', encoding="ISO-8859-1")
occurence = 0
while True:
line=fin.readline()
if not line:
break
m = re.search(regex1,line,re.IGNORECASE)
if m:
if (occurence==0):
print(file1)
occurence+=1
print(line)
fin.close()
##
## main
get_files_in_a_directory(dir2)
for i in filelist:
check_pattern_in_file(i, regex2)