Undoing character read from file

Emeka emekamicro at gmail.com
Sat Feb 18 04:38:22 CET 2012


Hello All,

Say I have something like this:

mfile = open("cc.txt", "rb")
mcount = 0
mset = False
while True:
c = mfile.read(1)
if c == "e" and mset is True and mcount == 0:
    print c
    mfile.seek(-1,1)
    mcount = 1
    continue
elif c == "e" and mset is False and mcount == 0:
    print c
    mfile.seek(-1, 0)
    mcount = 1
    continue
elif c == "e" and mcount == 1:
    print c
    mcount = 0
    continue
print c
 if mset is False:
    mset = True
if len(c) == 0:
    break

cc.txt

foor the this the been we hate to sh wiukr bee here today. But who are we
to question
him concerning this issue.

Is the above code the right way?

Regards, \Emeka

On Sat, Feb 18, 2012 at 5:17 AM, Emeka <emekamicro at gmail.com> wrote:

> Neil,
>
> Thanks. Could you throw a simple example?
>
> Regards, \Emeka
>
>
> On Fri, Feb 17, 2012 at 3:12 PM, Neil Cerutti <neilc at norwich.edu> wrote:
>
>> On 2012-02-16, MRAB <python at mrabarnett.plus.com> wrote:
>> > On 16/02/2012 23:10, Emeka wrote:
>> >> Hello All,
>> >>
>> >> I know about seek and tell while using readline. What about if I am
>> >> using read, and I want to undo the last character I just read(to return
>> >> it back to the stream). How do I achieve this?
>> >>
>> > Try:
>> >
>> >      f.seek(-1, 1)
>> >
>> > It seeks -1 relative to the current position (the second
>> > argument defaults to 0 for relative to start of file).
>>
>> Unless it's a stream opened in binary mode this will not work.
>> You'd need to maintain a n-character length buffer instead, with
>> n being the maximum number of characters you'd like to be able to
>> put back.
>>
>> --
>> Neil Cerutti
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
>
> --
> *Satajanus  Nig. Ltd
>
>
> *
>



-- 
*Satajanus  Nig. Ltd


*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120218/54dc4cef/attachment.html>


More information about the Python-list mailing list