user3077551
user3077551

Reputation: 79

How do I search for a word in a text document and display that line?

this is what i have at the moment. i need some help. im new to python and dont have that much knowledge on it. i need to search the file for a certain word word and then when the word occurs in the line it prints it.

file = open(input("please input your file name   "))
        word = input("please enter the value you want to search for   ")

    with open(file, 'r') as file:
        for line in file:
            if 'word' in line:
                print (line)

the problem im having is understanding what i need to assign to the with statement. to find the word and the line. at the moment i have file as file in the with statement. i do not know what else to put.

Upvotes: 0

Views: 81

Answers (1)

quazzieclodo
quazzieclodo

Reputation: 831

It should be (note that there are no quotes):

if word in line:

not

if 'searchphrase' in line:

Upvotes: 2

Related Questions