I want to know how I can search for a specific word in a .txt file using Python3. If the word 'pizza' is included in the .txt file, then it should (e.g.) import another python program.
Asked
Active
Viewed 311 times
-4
-
And what do your research efforts suggest you should use? – Steve Mar 17 '18 at 11:17
-
I found nothing similiar – RedDebian Mar 17 '18 at 11:19
-
I belive you can find the answer here https://stackoverflow.com/a/27914129/8362224 – LagSurfer Mar 17 '18 at 11:23
-
And if that doesn't help, [edit](https://stackoverflow.com/posts/49335342/edit) your question and add code that you tried, and why it didn't do what you wanted. See [ask] – Steve Mar 17 '18 at 11:25
1 Answers
0
You can iterate through each line in the file with a 'for' loop and check to see if the string is in the line:
f = open('file.txt', 'r')
for line in f:
if 'pizza' in line:
# do whatever you want here
print("Found it!")
else:
pass

Sam D
- 23
- 5