Pyton| search for string in big. Txt files and print | Sololearn: Learn to code for FREE!
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

22nd Mar 2017, 2:04 PM
Gabri
4 Answers
22nd Mar 2017, 6:33 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 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'
22nd Mar 2017, 7:34 PM
Ulisses Cruz
Ulisses Cruz - avatar
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'
22nd Mar 2017, 7:05 PM
Gabri
0
thanks! But why two if and not a common if else?
22nd Mar 2017, 8:02 PM
Gabri