473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 7390
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Adelein and Jeremy | last post by:
I am taking a second programming course in Java and am currently attempting to apply what I have learned using Python instead. One thing that is puzzling me is how to use an iterator. I am...
0
by: fishboy | last post by:
Howdy, Sorry if this is a double post. First try seemed to go into hyperspace. I'm working on a personal project. It's going to be a multipart binary attachment downloader that will search...
4
by: Robin Bryce | last post by:
Hi, I've been looking at generators from the context of event oriented web application development. I was thinking of submitting a version of http://www.wiretooth.com/eventhub_recipe.html as a...
16
by: Peter Otten | last post by:
To confuse a newbies and old hands alike, Bengt Richter wrote: > Need something more straightforward, e.g., a wrapped one-liner: > > >>> def guess(n=3): print ("You're right!", 'No more tries...
2
by: rawCoder | last post by:
Hi All, I have a *.cer file, a public key of some one and I want to encrypt some thing using this public key. Can someone point me to a sample code for Encrypting some file using...
10
by: Pierre Thibault | last post by:
Hello! I am currently trying to port a C++ code to python, and I think I am stuck because of the very different behavior of STL iterators vs python iterators. What I need to do is a simple...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
50
by: John Salerno | last post by:
I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I...
6
by: ccy56781 | last post by:
I'm writing to see calcuration process. And so, I can't catch StopIteration... What is mistake? def collatz(n): r= while n>1: r.append(n) n = 3*n+1 if n%2 else n/2
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.