On 22 January 2012 16:09, MRAB <pyt...@mrabarnett.plus.com> wrote: > On 22/01/2012 15:39, Arnaud Delobelle wrote: [...] >> Or more succintly (but not tested): >> >> >> sections = [ >> ("3", "section_1") >> ("5", "section_2") >> ("\xFF", "section_3") >> ] >> >> with open(input_path) as input_file: >> lines = iter(input_file) >> for end, path in sections: >> with open(path, "w") as output_file: >> for line in lines: >> if line>= end: >> break >> output_file.write(line) >> > Consider the condition "line >= end". > > If it's true, then control will break out of the inner loop and start > the inner loop again, getting the next line. > > But what of the line which caused it to break out? It'll be lost.
Of course you're correct - my reply was too rushed. Here's a hopefully working version (but still untested :). sections = [ ("3", "section_1") ("5", "section_2") ("\xFF", "section_3") ] with open(input_path) as input_file: line, lines = "", iter(input_file) for end, path in sections: with open(path, "w") as output_file: output_file.write(line) for line in lines: if line >= end: break output_file.write(line) -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list