On Tue, Jun 10, 2008 at 10:07 AM, dave selby &lt;<a href="mailto:dave6502@googlemail.com">dave6502@googlemail.com</a>&gt; wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi All,<br>
<br>
Up to now I when I need to write some data to a file I have been<br>
purposely using close()<br>
<br>
f = open(conf, &#39;w&#39;)<br>
f.writelines(lines)<br>
f.close()<br>
<br>
Is it as safe to use the following ....<br>
<br>
open(conf, &#39;w&#39;).writelines(lines)<br>
<br>
ie no close() to flush the data, but also not assigned an object name<br>
so am I right in thinking that as the object is &#39;reclaimed&#39; close() is<br>
automatically called ?<br>
<br>
Cheers<br>
<br>
Dave<br>
<font color="#888888"><br>
</font></blockquote><div>If you&#39;re using Python 2.5, you can use the new &quot;with&quot; -<br><blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">
<pre class="python"><span class="pykeyword">with</span> open(<span class="pystring">&quot;x.txt&quot;</span>) as f:<br>    data = f.read()<br>    do something <span class="pykeyword">with</span> data</pre></blockquote>which takes care of closing and possible exception handling automagically.&nbsp; Fredrik Lundh has a good article about it: <br>
<a href="http://effbot.org/zone/python-with-statement.htm">http://effbot.org/zone/python-with-statement.htm</a><br><br></div></div><br clear="all"><br>-- <br><a href="http://www.fsrtechnologies.com">www.fsrtechnologies.com</a>