while c = f.read(1)
- From: "Greg McIntyre" <greg@xxxxxxxxxxxx>
- Date: 18 Aug 2005 22:21:53 -0700
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
But I get a syntax error.
while c = f.read(1):
^
SyntaxError: invalid syntax
And read() doesn't work that way anyway because it returns '' on EOF
and '' != False. If I try:
f = open("blah.txt", "r")
while (c = f.read(1)) != '':
# ... work on c
I get a syntax error also. :(
Is this related to Python's expression vs. statement syntactic
separation? How can I be write this code more nicely?
Thanks
.
- Follow-Ups:
- Well, another try Re: while c = f.read(1)
- From: en.karpachov
- Re: while c = f.read(1)
- From: en.karpachov
- Re: while c = f.read(1)
- From: John Machin
- Re: while c = f.read(1)
- From: Donn Cave
- Re: while c = f.read(1)
- From: Robert Kern
- Re: while c = f.read(1)
- From: Bengt Richter
- Well, another try Re: while c = f.read(1)
- Prev by Date: Re: time.clock() problem under linux (precision=0.01s)
- Next by Date: Re: while c = f.read(1)
- Previous by thread: Traceback Questions
- Next by thread: Re: while c = f.read(1)
- Index(es):
Relevant Pages
|