while c = f.read(1)



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

.



Relevant Pages

  • Re: Password
    ... still I am getting the same error "syntax error in ALTER TABLE ... IF SQLEXEC(cnnSinaiPay, cSql) <= 0 ... The reason is that I tried to compact a MDB database and it does not ...
    (microsoft.public.fox.vfp.forms)
  • Re: rmdir when directory is not empty
    ... syntax error near unexpected token `newline' ... Prev by Date: ...
    (Fedora)
  • Re: rmdir when directory is not empty
    ... syntax error near unexpected token `newline' ... I'm dropping this thread; I'm sorry I interjected. ... Prev by Date: ...
    (Fedora)
  • RE: Formatting with commas
    ... >> If the field name in the report is also Wt-Kg change it to something else, ... >>> Thank you for your response Ofer. ... >>> it gave me syntax error. ... Prev by Date: ...
    (microsoft.public.access.reports)
  • Re: Hexadecimal numbers (SWI)
    ... I apologize for having said that SWI reports a syntax error, ... ERROR: Arithmetic: `a0/0' is not a function ... Prev by Date: ...
    (comp.lang.prolog)