468,537 Members | 2,445 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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(line)

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(line)

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

May 8 '06 #1
5 7219
tk****@hotmail.com wrote:
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 = open(fn)
for line in f:
if line[:1] in digits:
ProcessLine(line)
break
f.close()

A for instead of the inner while loop makes the f.next() call implicit.
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: break
else:
# only if StopIteration was not triggered
# and thus break not reached
ProcessLine(line)
f.close()
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?


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

Peter
May 8 '06 #2
On Mon, 08 May 2006 11:23:28 -0700, tkpmep wrote:
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)

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(line)
break
--
Steve R. Hastings "Vita est"
st***@hastings.org http://www.blarg.net/~steveha

May 8 '06 #3
sequence = ['','2']
for index, line in enumerate(sequence):
if line.isspace():continue
if line[:1].isdigit():
print 'index %s: starts with digit %s' % (index, line[:1])

May 8 '06 #4
to catch and recover from StopIterations, use this:

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

May 8 '06 #5
This is just what the doctor ordered. Thanks, as always, everyone!
By breaking out of the while loop as shown above.
Peter


May 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Robin Bryce | last post: by
2 posts views Thread by rawCoder | last post: by
50 posts views Thread by John Salerno | last post: by
6 posts views Thread by ccy56781 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.