-1

I am reading each line of a file and performing some operations on it. Sometimes the program throws an error due to some strange behavior in the network(It does SSH to a remote machine). This occurs once in a while. I want to catch this error and perform the same operations again on the same line. To be specific, I want to read the same line again. I am looking for something like this.

with open (file_name) as f:  
    for line in f:  
        try:    
            do this  
        except IndexError:  
            go back and read the same line again from the file.  
chepner
  • 497,756
  • 71
  • 530
  • 681
Rakesh Nittur
  • 445
  • 1
  • 7
  • 20
  • 1
    Why do you think you need to re-read the line? – jonrsharpe Aug 03 '15 at 16:20
  • My program throws index error due to some strange output that it receives from the network. This happens once in a while. May be once in 10 times. I think this can be solved if I can run the program again for the same line. – Rakesh Nittur Aug 03 '15 at 16:27
  • You already have the line, just re-use the same value. – interjay Aug 03 '15 at 16:27
  • I am looping over the files, not storing the lines. Just like the command 'continue', which skips an iteration, I am looking one to repeat the same iteration. – Rakesh Nittur Aug 03 '15 at 16:31

3 Answers3

4

As long as you’re within the block of your for loop, you still have access to that line (unless you modified it knowingly of course). So you don’t actually need to reread it from the file but you just still have it in memory.

You could for example try to “do this” repeatedly until it succeeds like this:

for line in f:
    while True:
        try:
            print(line)
            doThis()
        except IndexError:
            # we got an error, so let’s rerun this inner while loop
            pass
        else:
            # if we don’t get an error, abort the inner while loop
            # to get to the next line
            break
poke
  • 369,085
  • 72
  • 557
  • 602
1

You don't need to re-read the line. The line variable is holding your line. What you want to do is retry your operation in case it fails. One way would be to use a function and call the function from the function whenever it fails.

def do(line):
    try:
        pass # your "do this" code here
    except IndexError:
        do(line)

with open (file_name) as f:  
    for line in f:  
        do(line) 
Barry McNamara
  • 689
  • 9
  • 18
  • You're welcome. It looks like you're new to StackOverflow. If you found answers helpful, you should upvote them as well as accept the one you found most helpful. – Barry McNamara Aug 04 '15 at 00:13
  • Yes, you are right. This is my first question here. I still do not have enough reputation to upvote your solution. Sorrry – Rakesh Nittur Aug 04 '15 at 16:43
1

Python does not have a 'repeat' keyword that resets the execution pointer to the beginning of the current iteration. Your best approach is probably to look again at the structure of your code and break down 'do this' into a function that retries until it completes.

But if you are really set on emulating a repeat keyword as closely as possible, we can implement this by wrapping the file object in a generator

Rather than looping directly over the file, define a generator the yields from the file one line at a time, with a repeat option.

def repeating_generator(iterator_in):
    for x in iterator_in:
        repeat = True
        while repeat:
            repeat = yield x
            yield

Your file object can be wrapped with this generator. We pass a flag back into the generator telling it whether to repeat the previous line, or continue to the next one...

with open (file_name) as f:
    r = repeating_generator(f)  
    for line in r:  
        try:    
            #do this
            r.send(False) # Don't repeat
        except IndexError:  
            r.send(True) #go back and read the same line again from the file.  

Take a look at this question to see whats happening here. I don't think this is the most readable way of doing this, consider the alternatives first! Note that you will need Python 2.7 or later to be able to use this.

Community
  • 1
  • 1
James
  • 3,252
  • 1
  • 18
  • 33
  • Hi James, thanks a lot for your response. I can definitely implement this to solve my problem! You just did not provide me the solution but you did enlighten me when you said "Python does not have a 'repeat' keyword that resets the execution pointer to the beginning of the current iteration". – Rakesh Nittur Aug 03 '15 at 19:02