By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,172 Members | 727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

A file iteration question/problem

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.