finished #for section

This commit is contained in:
Mark Pilgrim
2009-07-18 13:31:19 -04:00
parent 6216bf3cac
commit 2d2a7fbf65
+3 -5
View File
@@ -224,13 +224,11 @@ ValueError: I/O operation on closed file.</samp>
line_number += 1
<a> print('{} {}'.format(line_number, a_line.rstrip())) <span class=u>&#x2462;</span></a></code></pre>
<ol>
<li>FIXME
<li>
<li>
<li>Using <a href=#with>the <code>with</code> pattern</a>, you safely open the file and let Python close it for you.
<li>This is it: to read a file one line at a time, use a <code>for</code> loop. That&#8217;s it. Besides having explicit methods like <code>read()</code>, <em>the file object is also an <a href=iterators.html>iterator</a></em> which spits out a single line every time you ask for a value.
<li>Using <a href=strings.html#formatting-strings>the <code>format()</code> string method, you can print out the line number and the line itself. (The <var>a_line</var> variable contains the complete line, carriage returns and all. The <code>rstrip()</code> string method removes the trailing whitespace, including the carriage return characters.)
</ol>
<p>FIXME
<pre class=screen>
<samp class=p>you@localhost:~/diveintopython3$ </samp><kbd class=pp>python3 examples/oneline.py</kbd>
<samp>1 Dora