Sep-16-2019, 05:23 AM
(Sep-16-2019, 05:11 AM)perfringo Wrote: EDITED: initial code contained error. Blank lines contain newline at end, so line must be stripped otherwise it will be truthy (contains newline). Therefore row.strip() should be used. Below code is fixed.
............
From screenshot I observe that there is blank line at the end of file. Problem can be easily mitigated by little defensive code - 'check whether line is empty'. Also I suggest to open files using 'with', among other goodies there is no need to close file - Python does it for you.
with open('filename.txt', 'r') as f: for row in f: if row.strip(): # the rest of code
Oh wow I did not know there is an alternative way to open files, and I was explicitly told to please remember to close them in order to free up the resources they used or something like that, I will look into your method , thanks for the advice and the fix on the code runs like a charm now with no hassle. Much appreciate perfringo