Python Forum
Searching for specific word in text files.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Searching for specific word in text files.
How do I search in a file for a specific word? It should ignore word containing that word, for example.

word to search = 'ello'
text file -
It shouldn't output anything file with the above text file
It should output 'success' with the above text file. How could I do this?
my_string = "hello\nthere"

start_index = my_string.find("ello")

start_index = my_string.find("x")
Opening files in text-mode for reading:
Together with Context-Manager:

All together in a function:

def has_pattern(file, pattern):
    with open(file, encoding="utf8") as fd:
        # inside this block the file is open for reading
        # read content -> find pattern -> return result_of_pattern != -1
        return != -1
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!

Possibly Related Threads…
Thread Author Replies Views Last Post
  Increment text files output and limit contains Kaminsky 1 208 Jan-30-2021, 06:58 PM
Last Post: bowlofred
  How to make a telegram bot respond to the specific word in a sentence? Metodolog 2 1,482 Dec-22-2020, 07:30 AM
Last Post: martabassof
  Split gps files based on time (text splitting) dervast 0 196 Nov-09-2020, 09:19 AM
Last Post: dervast
  Extracting data based on specific patterns in a text file K11 1 537 Aug-28-2020, 09:00 AM
Last Post: Gribouillis
  Find specific subdir, open files and find specific lines that are missing from a file tester_V 8 934 Aug-25-2020, 01:52 AM
Last Post: tester_V
  How to extract a single word from a text file buttercup 7 741 Jul-22-2020, 04:45 AM
Last Post: bowlofred
  Complex word search multiple files Kristenl2784 0 383 Jul-18-2020, 01:22 PM
Last Post: Kristenl2784
  Searching for a word Kristenl2784 0 384 Jul-09-2020, 01:06 AM
Last Post: Kristenl2784
  Outputting Sorted Text files Help charlieroberrts 1 423 Jul-05-2020, 08:37 PM
Last Post: menator01
  Read Multiples Text Files get specific lines based criteria zinho 5 809 May-19-2020, 12:30 PM
Last Post: zinho

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020