擅长:python、mysql、java
<p>你可以这样做:</p>
<pre class="lang-py prettyprint-override"><code>with open("yourfile.txt", "r") as f:
lines = f.readlines()
with open("yourfile.txt", "w") as f:
for line in lines:
if not line.startswith(YOUR_SEARCH_STRING):
f.write(line)
</code></pre>
<p>或者如果只想写一次文件:</p>
<pre class="lang-py prettyprint-override"><code>with open("yourfile.txt", "r") as f:
lines = f.readlines()
lines_to_write = [line for line in lines if not line.startswith(YOUR_SEARCH_SRING)]
with open("yourfile.txt", "w") as f:
f.write(''.join(lines_to_write))
</code></pre>