<p>我想打开一个文件并使用<code>f.seek()</code>和<code>f.tell()</code>读取每一行:</p>
<p>test.txt文件:</p>
<pre><code>abc
def
ghi
jkl
</code></pre>
<p>我的代码是:</p>
<pre><code>f = open('test.txt', 'r')
last_pos = f.tell() # get to know the current position in the file
last_pos = last_pos + 1
f.seek(last_pos) # to change the current position in a file
text= f.readlines(last_pos)
print text
</code></pre>
<p>它读取整个文件。</p>
<p>你为什么要用f.tell和f.seek?Python中的file对象是iterable,这意味着您可以在本地循环文件行,而不必担心其他问题:</p>
<pre><code>with open('test.txt','r') as file:
for line in file:
#work with line
</code></pre>