469,282 Members | 2,034 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

A file iteration question/problem

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
Apr 6 '08 #1
7 911
On Apr 6, 4:40*pm, tinn...@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)

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)

Or if you want the filename to be the argument of you function, wrap
it in a non-recursive function:

def fun(filename):
lines = iter(open(filename))
def recfun():
for line in lines:
# Do stuff
if condition:
recfun()
recfun()

HTH

--
Arnaud

Apr 6 '08 #2
Arnaud Delobelle <ar*****@googlemail.comwrote:
On Apr 6, 4:40┬*pm, tinn...@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)

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
Apr 7 '08 #3
ti*****@isbd.co.uk wrote:
Arnaud Delobelle <ar*****@googlemail.comwrote:
>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?
Don't speculate, try it.

Peter

Apr 7 '08 #4
En Mon, 07 Apr 2008 10:09:13 -0300, <ti*****@isbd.co.ukescribiˇ:
Arnaud Delobelle <ar*****@googlemail.comwrote:
>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?
Why not? Test and see what happens.

--
Gabriel Genellina

Apr 7 '08 #5
On Apr 7, 2:09*pm, tinn...@isbd.co.uk wrote:
Arnaud Delobelle <arno...@googlemail.comwrote:
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?
Try it! The keyword is iterator.

Here is an example of how this would work, but since you didn't
believe me I changed the context (strings not files) and I didn't make
it as simple as possible ;)

def reclist(string):
chariter = iter(string + ' ')
def rec():
l = []
word = ''
for c in chariter:
if c.isalnum():
word += c
elif word and (c.isspace() or c in '[]'):
l.append(word)
word = ''
if c == ']':
return l
elif c == '[':
l.append(rec())
return l
return rec()
>>reclist('40 and 2 eggs but no spam')
['40', 'and', '2', 'eggs', 'but', 'no', 'spam']
>>reclist('[[40 and 2] eggs] but [no spam]')
[[['40', 'and', '2'], 'eggs'], 'but', ['no', 'spam']]
>>>
--
Arnaud

Apr 7 '08 #6
ti*****@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
Apr 7 '08 #7
On Apr 7, 11:40*pm, John Nagle <na...@animats.comwrote:
tinn...@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

Apr 8 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Russell E. Owen | last post: by
35 posts views Thread by Raymond Hettinger | last post: by
reply views Thread by Danny Anderson | last post: by
7 posts views Thread by Shane | last post: by
3 posts views Thread by localpricemaps | last post: by
7 posts views Thread by ianenis.tiryaki | last post: by
9 posts views Thread by news.microsoft.com | last post: by
11 posts views Thread by andreyvul | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.