<div dir="ltr"><br><br><div class="gmail_quote">On Mon, Oct 26, 2009 at 2:12 PM, Luke Paireepinart <span dir="ltr">&lt;<a href="mailto:rabidpoobear@gmail.com">rabidpoobear@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br><br><div class="gmail_quote"><div class="im">On Mon, Oct 26, 2009 at 3:20 AM, Christian Witts <span dir="ltr">&lt;<a href="mailto:cwitts@compuscan.co.za" target="_blank">cwitts@compuscan.co.za</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

fInput = open(&#39;/path/to/log.file&#39;, &#39;rb&#39;)<br>
total_usage = 0<br>
for line in fInput:<br>
   total_usage += int(line.split(&#39; &#39;)[9].strip())<br>
print total_usage<br></blockquote></div><div><br>It&#39;s actually bad to assign a variable to the file object in this case (flinput = ....) because Python will automatically close a file after you&#39;re done with it if you iterate over it directly, but if you include a reference it will stay open until the python program ends or you explicitly call flinput.close().  It doesn&#39;t matter much in this example but in general it is good practice to either<br>

1) call foo.close() immediately after you&#39;re done using a file object, or<br>2) don&#39;t alias the file object and just over it directly so Python will auto-close it.<br><br>Therefore a better (and simpler) way to do the above would be:<br>

<br>total_usage = 0<br>for line in open(&#39;/path/to/log.file&#39;):<br>    total_usage += int(line.split(&#39; &#39;)[9])<br></div></div></blockquote><div><br>Hi Luke,<br><br>Your modification seems cleaner, you called the open function directly in the for loop.<br>
 <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="gmail_quote"><div><br>Also note you don&#39;t need to strip the input because int() coersion ignores whitespace anyway. And additionally you shouldn&#39;t be opening this in binary mode unless you&#39;re sure you want to, and I&#39;m guessing the log file is ascii so there&#39;s no need for the &#39;rb&#39;.  (reading is default so we don&#39;t specify an &#39;r&#39;.)<br>
</div></div></blockquote><div><br>The file is normal ascii text. Ii open it with no mode set, and that defaults to read-only.<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="gmail_quote"><div>
<br><br>And since I like list comprehensions a lot, I&#39;d probably do it like this instead:<br><br>total_usage = sum([int(line.split(&#39; &#39;)[9]) for line in open(&#39;/path/to/log.file&#39;)])<br><br>Which incidentally is even shorter, but may be less readable if you don&#39;t use list comprehensions often.<br>

<br>Also, the list comprehension version is likely to be more efficient, both because of the use of sum rather than repeated addition (sum is implemented in C) and because list comprehensions in general are a tad faster than explicit iteration, if i recall correctly (don&#39;t hold me to that though, I may be wrong.)<br>
</div></div></blockquote><div><br>I have read up on list comprehension and I seem to understand it&#39;s basics. I will play around with the different solutions on hand.<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="gmail_quote"><div>
</div><div class="im"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
Of course this has no error checking and or niceties, but I will leave that up to you.</blockquote></div><div>The same applies to my modifications.<br><br>Good luck, and let us know if you need anything else!<br><br>-Luke<br>
</div>
</div>
</blockquote></div><br>Thank you as always :-)<br><br clear="all"><br>-- <br>Best Regards,<br>bibimidi<br><br>Sent from Riyadh, 01, Saudi Arabia<br>
</div>