A file iteration question/problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tinnews@isbd.co.uk

    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
  • Arnaud Delobelle

    #2
    Re: A file iteration question/problem

    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(filen ame))
    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(filen ame))
    def recfun():
    for line in lines:
    # Do stuff
    if condition:
    recfun()
    recfun()

    HTH

    --
    Arnaud

    Comment

    • tinnews@isbd.co.uk

      #3
      Re: A file iteration question/problem

      Arnaud Delobelle <arnodel@google mail.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(filen ame))
      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

      Comment

      • Peter Otten

        #4
        Re: A file iteration question/problem

        tinnews@isbd.co .uk wrote:
        Arnaud Delobelle <arnodel@google mail.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(filen ame))
        >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

        Comment

        • Gabriel Genellina

          #5
          Re: A file iteration question/problem

          En Mon, 07 Apr 2008 10:09:13 -0300, <tinnews@isbd.c o.ukescribió:
          Arnaud Delobelle <arnodel@google mail.comwrote:
          >def recfun(lines):
          > for line in lines:
          > # Do stuff
          > if condition:
          > recfun(lines)
          >>
          >lines = iter(open(filen ame))
          >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

          Comment

          • Arnaud Delobelle

            #6
            Re: A file iteration question/problem

            On Apr 7, 2:09 pm, tinn...@isbd.co .uk wrote:
            Arnaud Delobelle <arno...@google mail.comwrote:
            def recfun(lines):
                for line in lines:
                    # Do stuff
                    if condition:
                        recfun(lines)
            >
            lines = iter(open(filen ame))
            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

            Comment

            • John Nagle

              #7
              Re: A file iteration question/problem

              tinnews@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

              Comment

              • Arnaud Delobelle

                #8
                Re: A file iteration question/problem

                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

                Comment

                Working...