while c = f.read(1)

John Machin sjmachin at lexicon.net
Fri Aug 19 18:37:08 EDT 2005


sp1d3rx at gmail.com wrote:
> 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
> intention.
> 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.
> 

Dear Sir or Madam,
I refer you to your recent post -- the one that started with "d'oh".
Regards,
John



More information about the Python-list mailing list