python – How to search for a string in text files?

python – How to search for a string in text files?

The reason why you always got True has already been given, so Ill just offer another suggestion:

If your file is not too large, you can read it into a string, and just use that (easier and often faster than reading and checking line per line):

with open(example.txt) as f:
    if blabla in f.read():
        print(true)

Another trick: you can alleviate the possible memory problems by using mmap.mmap() to create a string-like object that uses the underlying file (instead of reading the whole file in memory):

import mmap

with open(example.txt) as f:
    s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
    if s.find(blabla) != -1:
        print(true)

NOTE: in python 3, mmaps behave like bytearray objects rather than strings, so the subsequence you look for with find() has to be a bytes object rather than a string as well, eg. s.find(bblabla):

#!/usr/bin/env python3
import mmap

with open(example.txt, rb, 0) as file, 
     mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
    if s.find(bblabla) != -1:
        print(true)

You could also use regular expressions on mmap e.g., case-insensitive search: if re.search(br(?i)blabla, s):

As Jeffrey Said, you are not checking the value of check(). In addition, your check() function is not returning anything. Note the difference:

def check():
    with open(example.txt) as f:
        datafile = f.readlines()
    found = False  # This isnt really necessary
    for line in datafile:
        if blabla in line:
            # found = True # Not necessary
            return True
    return False  # Because you finished the search without finding

Then you can test the output of check():

if check():
    print(True)
else:
    print(False)

python – How to search for a string in text files?

Heres another way to possibly answer your question using the find function which gives you a literal numerical value of where something truly is

open(file, r).read().find()

in find write the word you want to find
and file stands for your file name

Leave a Reply

Your email address will not be published.