Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Neil Cerutti Newsgroups: comp.lang.python Subject: Re: Undoing character read from file Date: 17 Feb 2012 13:12:07 GMT Organization: Norwich University Lines: 22 Message-ID: <9q7217F6j0U3@mid.individual.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: individual.net 3gBO52OR7LU9tewDbsgIEw8j0PlsKzLkVHuvZ3cAAcidX7YTWH Cancel-Lock: sha1:psUtkojPBW7FeV5kQglOgH3B3Mo= User-Agent: slrn/0.9.9p1/mm/ao (Win32) Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20552 On 2012-02-16, MRAB 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