On Tue, Jun 10, 2008 at 10:07 AM, dave selby <<a href="mailto:dave6502@googlemail.com">dave6502@googlemail.com</a>> 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, 'w')<br>
f.writelines(lines)<br>
f.close()<br>
<br>
Is it as safe to use the following ....<br>
<br>
open(conf, 'w').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 'reclaimed' close() is<br>
automatically called ?<br>
<br>
Cheers<br>
<br>
Dave<br>
<font color="#888888"><br>
</font></blockquote><div>If you're using Python 2.5, you can use the new "with" -<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">"x.txt"</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. 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>