Sep-01-2020, 07:01 PM
Hi,
I have 100mb files and I’m looking for some lines (find1, find2, find3 and so on) in each file.
I’d like to print out to a file:
‘file name’ + find1 (if found) ------- I do not have problem doing it.
I have a problem with ‘else’,
I’d like to print to a file:
‘file name +find1+ not Found’ – one time.
I do not want to print ‘file name +find1+ not Found’ each time “foun1” not found in a line.
I cannot 'break' after first find1 is not found, I understand the code will exit from the 'for" loop.
I thought I could use veiable "notin_01 = '' ", I could redirect (and overwrite) all "not found" lines in to it and print the last 'not found' line. For some reason I cannot.
I tried to move
Thank you
I have 100mb files and I’m looking for some lines (find1, find2, find3 and so on) in each file.
I’d like to print out to a file:
‘file name’ + find1 (if found) ------- I do not have problem doing it.
I have a problem with ‘else’,
I’d like to print to a file:
‘file name +find1+ not Found’ – one time.
I do not want to print ‘file name +find1+ not Found’ each time “foun1” not found in a line.
I cannot 'break' after first find1 is not found, I understand the code will exit from the 'for" loop.
I thought I could use veiable "notin_01 = '' ", I could redirect (and overwrite) all "not found" lines in to it and print the last 'not found' line. For some reason I cannot.
I tried to move
print (notin_01) er_f.write(notin_01)left and right but could not find a solution.
Thank you
import os import os.path import re logDir = 'Some/Dir/' er_f = open('other/Dirs/Errors_By_Logs.txt','w') notin_01 = '' find1= 'find 1' for fn in os.listdir(logDir): if os.path.isfile(os.path.join(logDir, fn)): print(" File name : ", fn) dir_f = logDir+fn with open(dir_f) as each_dirf : for ln_f in each_dirf : ln_f=ln_f.rstrip() if find1 in ln_f : er_f.write(ln_f) print (dir_f+ '-'+ find1) else : notin_01 = (dir_f + "-" + "Cannot FIND - "+ "-"+ find1) print (notin_01) er_f.write(notin_01) er_f.close()