Using StopIteration

T

tkpmep

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
 
P

Peter Otten

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
 
S

Steve R. Hastings

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
 
V

vbgunz

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])
 
V

vbgunz

to catch and recover from StopIterations, use this:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top