Find a word in a file using Python-DecodingDevOps

Find a word in a file using Python

STEP 1: create a new text file using notepad and write some appropriate sentences in the respective text file and save the text file in the ‘.txt’ format.
STEP 2: Open the text file using the open() function in the read mode and read the text in the following text file using the read function.
STEP 3: split the text using the split function. The split() method is used to split a string into an array of substrings and returns the new array.
STEP 4: using the for loop which is used for iteration and to check the particular word assigning the i value to the word which is to be checked and print the following word in a text file having some appropriate sentences.

with open(r"textfile.txt") as file
content = file.read()
sentence = content.split(' ')
for i in sentence:
          if i == "word" :
                  print i

OUTPUT:  The searched “word ” is the output for the following program.

Leave a Reply

Your email address will not be published. Required fields are marked *