Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > On Apr 6, 4:40 pm, [EMAIL PROTECTED] wrote: > > I want to iterate through the lines of a file in a recursive function > > so I can't use:- > > > > f = open(listfile, 'r') > > for ln in f: > > > > because when the function calls itself it won't see any more lines in > > the file. E.g. more fully I want to do somthing like:- > > > > def recfun(f) > > while True: > > str = readline(f) > > if (str == "") > > break; > > # > > # do various tests > > # > > if <something>: > > recfun(f) > > > > Is there no more elegant way of doing this than that rather clumsy > > "while True" followed by a test? > > > > -- > > Chris Green > > You could use an iterator over the lines of the file: > > def recfun(lines): > for line in lines: > # Do stuff > if condition: > recfun(lines) > > lines = iter(open(filename)) > recfun(lines) > Does that work though? If you iterate through the file with the "for line in lines:" in the first call of recfun(lines) you surely can't do "for line in lines:" and get any sort of sensible result in recursive calls of recfun(lines) can you?
-- Chris Green -- http://mail.python.org/mailman/listinfo/python-list