<br><br><div class="gmail_quote">On Wed, Dec 9, 2009 at 2:46 PM, Lie Ryan <span dir="ltr"><<a href="mailto:lie.1296@gmail.com">lie.1296@gmail.com</a>></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't work unless you have STDOUT redirected to your file. It<br>
would be better just to do<br>
fobj.write('\n'.join(all) + '\n')<br>
</blockquote>
<br></div>
except that if you're joining a very long string, you'll have python copy the whole string again.<br>
<br>
better to just redirect print<br>
print >> fobj, '\n'.join(all)<br></blockquote><div><br></div><div>I'm sorry, but this just seems like kind of an unreadable way to do this. You'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 "all" 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('\n'.join(all))</div><div>fobj.write('\n')</div><div><br>
</div><div>Then it's still absolutely clear that you're trying to get a newline there and it'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're talking about, you can write while getting user input.<br>
<br>
with open(...) as fobj:<br>
entry = ''<br>
while entry != '.'<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're relying on the implicit newline created by reading in the user's string from raw_input.</div>
<div>So I don'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're going to get confused.</div>
<div><br></div><div>-Luke </div></div><br>