0
Pyton| search for string in big. Txt files and print
Hi guys! I have a problem trying to figure out how to make a program which search for a defined string in multiple files and then print the line. Something the 'grep' function in Linux. These files are pretty big. Could you please help me? Thanks
4 Réponses
+ 6
check out my script:
https://code.sololearn.com/cl9w7fpqgX6g/?ref=app
+ 6
do this:
with open('aa.txt') as searchfile:
found = False
for line in searchfile:
if 'hello' in line:
print line
found = True
if not found: print 'nothing found'
0
Hey thanks for reply. I ended with this code (before you posted), but now the else part print for every line in search file. How can I just make it print one time?
With open('aa.Txt', 'r') as searchfile:
For line in searchfile:
If "hello" in line:
Print line
Else:
Print' nothing found'
0
thanks! But why two if and not a common if else?