Using StopIteration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tkpmep@hotmail.com

    #1

    Using StopIteration

    I create list of files, open each file in turn, skip past all the blank
    lines, and then process the first line that starts with a number (see
    code below)

    filenames=glob. glob("C:/*.txt")
    for fn in filenames:
    f =file(fn)
    line = " "
    while line[0] not in digits:
    line = f.next()
    ProcessLine(lin e)

    If a file has only blank lines, the while loop terminates with a
    StopIteration. How can I just close this file andd skip to the next
    file if a StopIteration is raised? I tried the following:

    filenames=glob. glob("C:/*.txt")
    for fn in filenames:
    f =file(fn)
    line = " "
    while line[0] not in digits:
    try:
    line = f.next()
    except StopIteration:
    f.close()
    continue

    ProcessLine(lin e)

    but got only a ValueError: I/O operation on closed file for line =
    f.next(). It appears that the continue is taking me back to the top of
    the while loop. How can I get back to the top of the for loop?

    Thanks in advance

    Thomas Philips

  • Peter Otten

    #2
    Re: Using StopIteration

    tkpmep@hotmail. com wrote:
    [color=blue]
    > I create list of files, open each file in turn, skip past all the blank
    > lines, and then process the first line that starts with a number (see
    > code below)
    >
    > filenames=glob. glob("C:/*.txt")
    > for fn in filenames:[/color]
    f = open(fn)
    for line in f:
    if line[:1] in digits:
    ProcessLine(lin e)
    break
    f.close()

    A for instead of the inner while loop makes the f.next() call implicit.
    [color=blue]
    > If a file has only blank lines, the while loop terminates with a
    > StopIteration. How can I just close this file andd skip to the next
    > file if a StopIteration is raised? I tried the following:
    >
    > filenames=glob. glob("C:/*.txt")
    > for fn in filenames:
    > f =file(fn)
    > line = " "
    > while line[0] not in digits:
    > try:
    > line = f.next()
    > except StopIteration:[/color]
    break
    else:
    # only if StopIteration was not triggered
    # and thus break not reached
    ProcessLine(lin e)
    f.close()
    [color=blue]
    > but got only a ValueError: I/O operation on closed file for line =
    > f.next(). It appears that the continue is taking me back to the top of
    > the while loop. How can I get back to the top of the for loop?[/color]

    By breaking out of the while loop as shown above.
    (all changes untested)

    Peter

    Comment

    • Steve R. Hastings

      #3
      Re: Using StopIteration

      On Mon, 08 May 2006 11:23:28 -0700, tkpmep wrote:[color=blue]
      > I create list of files, open each file in turn, skip past all the blank
      > lines, and then process the first line that starts with a number (see
      > code below)[/color]


      Here is what I suggest for you:


      filenames=glob. glob("C:/*.txt")
      for fn in filenames:
      for line in open(fn):
      if line[0] in digits:
      ProcessLine(lin e)
      break


      --
      Steve R. Hastings "Vita est"
      steve@hastings. org http://www.blarg.net/~steveha

      Comment

      • vbgunz

        #4
        Re: Using StopIteration

        sequence = ['','2']
        for index, line in enumerate(seque nce):
        if line.isspace(): continue
        if line[:1].isdigit():
        print 'index %s: starts with digit %s' % (index, line[:1])

        Comment

        • vbgunz

          #5
          Re: Using StopIteration

          to catch and recover from StopIterations, use this:

          try:
          raise StopIteration
          except StopIteration:
          print 'caught StopIteration!' # verbose: sys.exc_info() requires
          import sys

          Comment

          • tkpmep@hotmail.com

            #6
            Re: Using StopIteration

            This is just what the doctor ordered. Thanks, as always, everyone!
            [color=blue]
            > By breaking out of the while loop as shown above.[/color]
            [color=blue]
            >
            > Peter[/color]

            Comment

            Working...