A file iteration question/problem

Arnaud Delobelle arnodel at googlemail.com
Tue Apr 8 07:38:32 CEST 2008


On Apr 7, 11:40 pm, John Nagle <na... at animats.com> wrote:
> tinn... at isbd.co.uk 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)
>
>      Don't do that; Python doesn't have tail recursion and you'll hit the
> stack limit.
>
>                                 John Nagle

This function is not tail recursive (the recursive call is in a loop).

--
Arnaud




More information about the Python-list mailing list