擅长:python、mysql、java
<p>最好你应该习惯于在打开文件时使用<code>with</code>,这样当你处理完文件后它就会自动关闭。但最主要的是使用<code>in</code>在另一个字符串中搜索字符串。</p>
<pre><code>with open('search.txt') as file:
contents = file.read()
search_word = input("enter a word you want to search in file: ")
if search_word in contents:
print ('word found')
else:
print ('word not found')
</code></pre>