I often write the results of geoprocessing scripts to a text file. After doing some research, I learned I'm not using best practices for closing the file. I'm not opening the file using the with keyword, so the file may not close if there is an error. If there is an error, I need to be able to write the error message to the text file. It looks like I could use the finally keyword or with keyword to ensure the file closes.
Which of these options is better? And if I've made any errors in how I think I could improve my implementation, please let me know. Thanks!
Method #1
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># log file to write to</SPAN>
logFile <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'path\tofile\fileName.txt'</SPAN>
<SPAN class="comment token"># open file in write mode</SPAN>
report <SPAN class="operator token">=</SPAN> open<SPAN class="punctuation token">(</SPAN>logFile<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'w'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># geoprocessing stuff</SPAN>
<SPAN class="comment token"># write to report</SPAN>
report<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'some message'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN> Exception <SPAN class="keyword token">as</SPAN> e<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># get line number and error message</SPAN>
report<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'an error message'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">finally</SPAN><SPAN class="punctuation token">:</SPAN>
report<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Method #2
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># log file to write to</SPAN>
logFile <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'path\tofile\fileName.txt'</SPAN>
<SPAN class="comment token"># open file in write mode</SPAN>
<SPAN class="keyword token">with</SPAN> open<SPAN class="punctuation token">(</SPAN>logFile<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'w'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> f<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># geoprocessing stuff</SPAN>
<SPAN class="comment token"># write to report</SPAN>
f<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'some message'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN> Exception <SPAN class="keyword token">as</SPAN> e<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># get line number and error message</SPAN>
<SPAN class="keyword token">with</SPAN> open<SPAN class="punctuation token">(</SPAN>logFile<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'a'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> f<SPAN class="punctuation token">:</SPAN>
f<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'an error message'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>