while c = f.read(1)

sp1d3rx at gmail.com sp1d3rx at gmail.com
Fri Aug 19 22:18:01 CEST 2005

Alright, everyone seems to have gone off on a tangent here, so I'll try
to stick to your code...
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


That's because you are using an assignment operator instead of a
comparison operator. It should have been written like this:

while c == f.read(1):

that would be written correctly, though I don't think that is your
Try this novel implementation, since nobody has suggested it yet.
import mmap

f  = open("blah.txt", 'r+') #opens file for read/write
c = mmap.mmap(f.fileno(),0) #maps the file to be used as memory map...

while c.tell() < c.size():
    print c.read_byte()
That accomplishes the same thing.

More information about the Python-list mailing list