On Mon, Oct 26, 2009 at 2:12 PM, Luke Paireepinart <rabidpoob...@gmail.com>wrote:
> > > On Mon, Oct 26, 2009 at 3:20 AM, Christian Witts > <cwi...@compuscan.co.za>wrote: > >> fInput = open('/path/to/log.file', 'rb') >> total_usage = 0 >> for line in fInput: >> total_usage += int(line.split(' ')[9].strip()) >> print total_usage >> > > It's actually bad to assign a variable to the file object in this case > (flinput = ....) because Python will automatically close a file after you'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't matter much in this example but in general it > is good practice to either > 1) call foo.close() immediately after you're done using a file object, or > 2) don't alias the file object and just over it directly so Python will > auto-close it. > > Therefore a better (and simpler) way to do the above would be: > > total_usage = 0 > for line in open('/path/to/log.file'): > total_usage += int(line.split(' ')[9]) > Hi Luke, Your modification seems cleaner, you called the open function directly in the for loop. > > Also note you don't need to strip the input because int() coersion ignores > whitespace anyway. And additionally you shouldn't be opening this in binary > mode unless you're sure you want to, and I'm guessing the log file is ascii > so there's no need for the 'rb'. (reading is default so we don't specify an > 'r'.) > The file is normal ascii text. Ii open it with no mode set, and that defaults to read-only. > > And since I like list comprehensions a lot, I'd probably do it like this > instead: > > total_usage = sum([int(line.split(' ')[9]) for line in > open('/path/to/log.file')]) > > Which incidentally is even shorter, but may be less readable if you don't > use list comprehensions often. > > 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't hold me to that though, I > may be wrong.) > I have read up on list comprehension and I seem to understand it's basics. I will play around with the different solutions on hand. > >> Of course this has no error checking and or niceties, but I will leave >> that up to you. > > The same applies to my modifications. > > Good luck, and let us know if you need anything else! > > -Luke > Thank you as always :-) -- Best Regards, bibimidi Sent from Riyadh, 01, Saudi Arabia
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor