擅长:python、mysql、java
<p>另一种选择是,您可以在读取文件本身时<code>search</code>:</p>
<pre><code>search_word = input("enter a word you want to search in file: ")
if search_word in open('search.txt').read():
print("word found")
else:
print("word not found")
</code></pre>
<p>要缓解可能的内存问题,请使用这里所回答的<code>mmap.mmap()</code><a href="https://stackoverflow.com/questions/4940032/search-for-string-in-txt-file-python">related question</a></p>