Undoing character read from file

Neil Cerutti neilc at norwich.edu
Fri Feb 17 08:12:07 EST 2012


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



More information about the Python-list mailing list