<div>Hello<br clear="all"></div><div><br></div><div>May I, a mere beginner,  be so bold as to suggest a modification of your excellent Python Tutorial?</div><div><br></div><div>I think certain modifications of section 7.2 (<a href="http://docs.python.org/tutorial/inputoutput.html">http://docs.python.org/tutorial/inputoutput.html</a>) would be of great assistance to beginners.</div>
<div><br></div><div>First of all, the order in which the different file operations are presented could be changed for greater comprehension. Section 7.2 begins the beginners&#39; introduction to file objects (after a quick introduction to opening/creating files) by giving a rather sizable description of read functions, with examples. Since the beginner at this stage hasn&#39;t learned the first thing about writing to files, she is unlikely to have any test files on which to perform the given read functions. This section on reading files is therefore redundant until the beginner has got to the section further on that describes how to write files, after which she must return to the start of the section to do the read samples and effectively read the section backwards. It would be more helpful to have the write tutorial before the read tutorial.</div>
<div><br></div><div>In addition to this confusion of ordering, I had some trouble following the given read and write examples.</div><div><br></div><div>Please refer to this example transcript of my attempt to follow the instructions in section 7.2:</div>
<div><br></div><div>* STEP 1 - I open a file for reading and writing and write to the file</div><div><br></div><div>&gt;&gt;&gt; f = open(&#39;/tmp/workfile&#39;, &#39;r+&#39;)<br>&gt;&gt;&gt; print f<br>&lt;open file &#39;/tmp/workfile&#39;, mode &#39;r+&#39; at 0xb76a31d8&gt;<br>
&gt;&gt;&gt; f.read()<br>&#39;&#39;<br>&gt;&gt;&gt; f.write(&#39;this is first line of file\n&#39;)<br></div><div><br></div><div>* STEP 2 - I attempt to read what I&#39;ve written, but without success</div><div><br></div>
<div>&gt;&gt;&gt; f.readline()<br>&#39;&#39;<br>&gt;&gt;&gt; f.readlines()<br>[]<br></div><div><br></div><div>* STEP 3 - Bemused, I try writing and reading some more</div><div><br></div><div>&gt;&gt;&gt; f.write(&#39;this is a test&#39;)<br>
&gt;&gt;&gt; f.readlines()<br>[]<br></div><div><br></div><div>* BREAKOUT - Unable to find any explanation in 7.2 I go for help elsewhere</div><div>                        After some time, I find it in the Python Tutor mail list</div>
<div>                        (here: <a href="http://mail.python.org/pipermail/tutor/2003-July/024154.html">http://mail.python.org/pipermail/tutor/2003-July/024154.html</a>)</div><div><br></div><div>* STEP 4 - Using f.close() I successfully read what I&#39;ve written</div>
<div>                 But only one of my previously written lines is there</div><div><br></div><div>&gt;&gt;&gt; f.close()<br>&gt;&gt;&gt; f = open(&#39;/tmp/workfile&#39;, &#39;r+&#39;)<br>&gt;&gt;&gt; f.readlines()<br>[&#39;this is first line of file\n&#39;]<br>
</div><div><br></div><div>* STEP 5 - Bemused, I try writing another line</div><div>                 And use the knowledge gleaned from the Python Tutor Mail list to reset the file pointer</div><div><br></div><div>&gt;&gt;&gt; f.write(&#39;this is second line\n&#39;)<br>
&gt;&gt;&gt; f.readlines()<br>[]<br>&gt;&gt;&gt; f.seek(0)<br>&gt;&gt;&gt; f.readlines()<br>[&#39;this is first line of file\n&#39;]<br></div><div><br></div><div>* STEP 6 - But where are the other lines I&#39;ve written?</div>
<div><br></div><div>&gt;&gt;&gt; f.readlines()<br>[]</div><div><br></div><div>* STEP 7 - Bemused, I write yet another line to the file, reset the pointer and read it back</div><div><br>&gt;&gt;&gt; f.write(&#39;this is what where?\n&#39;)<br>
&gt;&gt;&gt; f.seek(0)<br>&gt;&gt;&gt; f.readlines()<br>[&#39;this is first line of file\n&#39;, &#39;this is what where?\n&#39;]<br>&gt;&gt;&gt; <br><br></div><div>For what to me is an inexplicable reason, my last attempt at writing to this file seemed to be successful: it read back. But all my previous attempts to write a second line have been lost in the ether. Now I&#39;m sure I&#39;ll work this out eventually. But perhaps the tutorial could clarify some of these points.<br>
</div><div><br></div><div>I would like to suggest that one way to improve 7.2 would be to include at the beginning of the section, after a simple introduction of write and read with basic one line examples, a description of the file pointer and why its important in reading back what one has written. It may then use multiple line writes and reads to demonstrate how these differ. And perhaps then some word on these phantom writes?</div>
<div><br></div><div>Kind regards</div><div><br></div><div>Mark.</div>