擅长:python、mysql、java
<p>你说得对,<a href="https://docs.python.org/2/library/fileinput.html" rel="nofollow noreferrer">^{<cd1>} module</a>正是你所需要的:</p>
<pre><code>import fileinput
def blank_line(filename, lineno):
f = fileinput.input(files=[filename], inplace=True)
for line in f:
if fileinput.lineno() == lineno: # note: line numbers start at 1, not 0
line = ""
print line.rstrip("\n") # Output is redirected to the current line of the file
f.close()
</code></pre>
<p>请注意,Python3在这里有几个优点:<code>fileinput</code>支持上下文管理器(<code>with</code>语句),而新的<code>print()</code>函数允许我们保持行的原样(而不是总是在末尾添加新行或空格)。在</p>