Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1002395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:49:35+00:00 2026-05-16T07:49:35+00:00

Python’s f.tell doesn’t work as I expected when you iterate over a file with

  • 0

Python’s f.tell doesn’t work as I expected when you iterate over a file with f.next():

>>> f=open(".bash_profile", "r")
>>> f.tell()
0
>>> f.next()
"alias rm='rm -i'\n"
>>> f.tell()
397
>>> f.next()
"alias cp='cp -i'\n"
>>> f.tell()
397
>>> f.next()
"alias mv='mv -i'\n"
>>> f.tell()
397

Looks like it gives you the position of the buffer rather than the position of what you just got with next().

I’ve previously used the seek/tell trick to rewind one line when iterating over a file with readline(). Is there a way to rewind one line when using next()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:49:36+00:00Added an answer on May 16, 2026 at 7:49 am

    No. I would make an adapter that largely forwarded all calls, but kept a copy of the last line when you did next and then let you call a different method to make that line pop out again.

    I would actually make the adapter be an adapter that could wrap any iterable instead of a wrapper for file because that sounds like it would be frequently useful in other contexts.

    Alex’s suggestion of using the itertools.tee adapter also works, but I think writing your own iterator adapter to handle this case in general would be cleaner.

    Here is an example:

    class rewindable_iterator(object):
        not_started = object()
    
        def __init__(self, iterator):
            self._iter = iter(iterator)
            self._use_save = False
            self._save = self.not_started
    
        def __iter__(self):
            return self
    
        def next(self):
            if self._use_save:
                self._use_save = False
            else:
                self._save = self._iter.next()
            return self._save
    
        def backup(self):
            if self._use_save:
                raise RuntimeError("Tried to backup more than one step.")
            elif self._save is self.not_started:
                raise RuntimeError("Can't backup past the beginning.")
            self._use_save = True
    
    
    fiter = rewindable_iterator(file('file.txt', 'r'))
    for line in fiter:
        result = process_line(line)
        if result is DoOver:
            fiter.backup()
    

    This wouldn’t be too hard to extend into something that allowed you to backup by more than just one value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python's subprocess module by default passes all open file descriptors to any child processes
Python 3 really complicated the whole file reading process, when you have a binary
Python beginner here. I have a text file that is sorted into columns: fields
Python allows aliasing of imports, through ...as <ALIAS> clauses in the import statement, like
Python beginner >>> import oauth2 Traceback (most recent call last): File , line 1,
Python doesn't support complicated anonymous functions. What's a good alternative? For example: class Calculation:
python manage.py dumpdata modelName > file.json . created an empty database for a user
Python's file.read() function won't read anything. It always returns '' no matter what's inside
Python 2.6 and beyond has the ability to directly execute a .zip file if
Python is installed in a local directory. My directory tree looks like this: (local

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.