Bengt Richter wrote: > On 18 Aug 2005 22:21:53 -0700, "Greg McIntyre" <[EMAIL PROTECTED]> wrote: > > >>I have a Python snippet: >> >> f = open("blah.txt", "r") >> while True: >> c = f.read(1) >> if c == '': break # EOF >> # ... work on c >> >>Is some way to make this code more compact and simple? It's a bit >>spaghetti. >> >>This is what I would ideally like: >> >> f = open("blah.txt", "r") >> while c = f.read(1): >> # ... work on c >> > > How about (untested): > > for c in iter((lambda f=open('blah.txt', 'r'): f.read(1)), ''): > # ... work on c > :-) Bengt, did you read on to the bit where the OP wanted to do it "more nicely"? YMMV, but I think you've strayed into "pas devant les enfants" territory. (-:
Cheers, John -- http://mail.python.org/mailman/listinfo/python-list