while c = f.read(1)
John Machin
sjmachin at lexicon.net
Fri Aug 19 02:31:47 EDT 2005
Bengt Richter wrote:
> On 18 Aug 2005 22:21:53 -0700, "Greg McIntyre" <greg at puyo.cjb.net> 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
More information about the Python-list
mailing list