Thanks all. <div>I think my fundamental error was in thinking of <i>menu.tx</i>t as the file and <i>inp</i> as containing it&#39;s contents in some way. Much more helpful to think of <i>inp</i> as the file and then do things to it.<div>
<br></div><div>Thanks again</div><div><br></div><div>Neil</div><div><br clear="all">This email is confidential and intended for addressee only .<br>
<br><br><div class="gmail_quote">On Mon, Jun 21, 2010 at 1:37 AM, Alex Hall <span dir="ltr">&lt;<a href="mailto:mehgcap@gmail.com">mehgcap@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On 6/20/10, Steven D&#39;Aprano &lt;<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>&gt; wrote:<br>
&gt; On Mon, 21 Jun 2010 09:02:55 am Alex Hall wrote:<br>
&gt;&gt; On 6/20/10, Neil Thorman &lt;<a href="mailto:neil.thorman@gmail.com">neil.thorman@gmail.com</a>&gt; wrote:<br>
&gt; [...]<br>
&gt;&gt; &gt;&gt;&gt;&gt;inp = file(&quot;menu.txt&quot;, &quot;r&quot;)<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; *What is inp? What does it now contain?*<br>
&gt;&gt;<br>
&gt;&gt; It is now a reference to the location of the txt file.<br>
&gt;<br>
&gt; [pedantic]<br>
&gt; No, it&#39;s actually an abstract data structure that wraps the actual file<br>
&gt; itself. A reference to the *location* of the file would be:<br>
&gt;<br>
&gt; inp = &quot;menu.txt&quot;<br>
&gt;<br>
&gt; Locations are strings. File objects are file objects.<br>
&gt; [/pedantic]<br>
&gt;<br>
&gt; But in a practical sense, pay no attention to the man behind the curtain<br>
&gt; (the implementation details). inp is a file in every way which matters,<br>
&gt; just like after:<br>
&gt;<br>
&gt; n = 42<br>
&gt; x = 1.234<br>
&gt;<br>
&gt; n is an int and x a float in every way which matters.<br>
&gt;<br>
&gt;<br>
&gt;&gt; Python calls<br>
&gt;&gt; these file &quot;objects&quot;, where an object is just something on which you<br>
&gt;&gt; can call functions. If you had a dog object you might call a &quot;bark&quot;<br>
&gt;&gt; method; here, we have a file object, so we can see what the file is.<br>
&gt;&gt; inp is not the file itself, just as any object is not the full<br>
&gt;&gt; object&#39;s info but rather a pointer to where that info is. If you were<br>
&gt;&gt; to print inp, I think you would get a memory address.<br>
&gt;<br>
&gt; You would get a string printed to the console, like printing *anything*.<br>
&gt;<br>
&gt;&gt;&gt;&gt; print inp<br>
&gt; &lt;open file &#39;foo&#39;, mode &#39;w&#39; at 0xb7d3a250&gt;<br>
</div></div>Oh right, the object&#39;s toString method (or whatever Python calls this;<br>
Java and Javascript call it toString, and it exists for all objects).<br>
<div class="im">&gt;<br>
&gt; That string happens to contain a hex number which looks like it could be<br>
&gt; a memory address, but that&#39;s an implementation detail because CPython<br>
&gt; doesn&#39;t sufficiently abstract its objects from the underlying C<br>
&gt; implementation.<br>
&gt;<br>
&gt; Python file objects aren&#39;t &quot;files&quot; only in the sense that they exist in<br>
&gt; memory rather than on disk in the file system, but other than that, I<br>
&gt; believe your explanation is at too low a level to be helpful. Neil said<br>
&gt; he&#39;s a beginner who hasn&#39;t done any programming since BASIC on an<br>
&gt; Acorn, and you&#39;re talking about low-level details like memory<br>
&gt; locations. Let me guess, you&#39;re also a C programmer?<br>
</div>Good point, and sorry for going into too much detail, much of which is<br>
not on the mark anyway. :) No, I have hardly touched c++, but I am<br>
starting my final year of a computer science degree in a few months so<br>
I have had all the details of objects and how the computer actually<br>
accesses them in several classes.<br>
<div class="im">&gt;<br>
&gt; As far as coding in Python is concerned, inp = file(pathname) creates a<br>
&gt; file object, which *is* a file in all practical sense. Everything else<br>
&gt; is just an implementation detail, which could change depending on the<br>
&gt; version of Python, the operating system, and the underlying hardware.<br>
</div>Very true.<br>
<div class="im">&gt;<br>
&gt;<br>
&gt; [...]<br>
&gt;&gt; &gt;&gt;&gt;&gt;print inp.readlines()<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; [&#39;spam &amp; eggs\n&#39;, &#39;spam &amp; chips\n&#39;, &#39;spam &amp; spam&#39;]<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; but if I do it again I get:<br>
&gt;&gt; &gt;&gt;&gt;&gt; print inp.readlines()<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; []<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; I&#39;m baffled, why is inp now empty?<br>
&gt;&gt;<br>
&gt;&gt; I suspect you have hit the end of the file.<br>
&gt;<br>
&gt; Yes. readlines reads from the current file position, like all read<br>
&gt; operations. To read all the text again, you have to reset the file<br>
&gt; position with seek:<br>
&gt;<br>
&gt; inp.seek(0)<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; Steven D&#39;Aprano<br>
&gt; _______________________________________________<br>
&gt; Tutor maillist  -  <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
&gt; To unsubscribe or change subscription options:<br>
&gt; <a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
&gt;<br>
<br>
<br>
</div><div class="im">--<br>
Have a great day,<br>
Alex (msg sent from GMail website)<br>
<a href="mailto:mehgcap@gmail.com">mehgcap@gmail.com</a>; <a href="http://www.facebook.com/mehgcap" target="_blank">http://www.facebook.com/mehgcap</a><br>
</div><div><div></div><div class="h5">_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br></div></div>