<br><br><div class="gmail_quote">On Wed, Dec 9, 2009 at 2:46 PM, Lie Ryan <span dir="ltr">&lt;<a href="mailto:lie.1296@gmail.com">lie.1296@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 class="im">On 12/10/2009 6:12 AM, Luke Paireepinart wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
This won&#39;t work unless you have STDOUT redirected to your file.  It<br>
would be better just to do<br>
  fobj.write(&#39;\n&#39;.join(all) + &#39;\n&#39;)<br>
</blockquote>
<br></div>
except that if you&#39;re joining a very long string, you&#39;ll have python copy the whole string again.<br>
<br>
better to just redirect print<br>
print &gt;&gt; fobj, &#39;\n&#39;.join(all)<br></blockquote><div><br></div><div>I&#39;m sorry, but this just seems like kind of an unreadable way to do this.  You&#39;re implicitly relying on print adding a newline to the end of every output.</div>
<div>You could also just append an empty string to the end of &quot;all&quot; and thus another newline would be added.</div><div>Either approach is less clear than the concatenation.  I guess it just depends on the size of your string. </div>
<div>The most readable approach while still being efficient would probably be just to write a newline after the previous write.</div><div>fobj.write(&#39;\n&#39;.join(all))</div><div>fobj.write(&#39;\n&#39;)</div><div><br>
</div><div>Then it&#39;s still absolutely clear that you&#39;re trying to get a newline there and it&#39;s not a side-effect of some other function.</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<br>
but since this is a file object we&#39;re talking about, you can write while getting user input.<br>
<br>
with open(...) as fobj:<br>
    entry = &#39;&#39;<br>
    while entry != &#39;.&#39;<br>
        fobj.write(raw_input())<div><div></div><div class="h5"><br></div></div></blockquote><div>Again, the file will only have a newline at the end in this situation because you&#39;re relying on the implicit newline created by reading in the user&#39;s string from raw_input.</div>
<div>So I don&#39;t think this is a good example to give in this situation, because as soon as the user starts splitting / stripping / parsing raw_input and then trying to write it back out</div><div>they&#39;re going to get confused.</div>
<div><br></div><div>-Luke </div></div><br>