1

I am writing a Python (specifically 3.x) script to read names from a file and process them. Lets assume I have the following text in the file names.txt which looks something like this:

Rebecca Rivera
Ralph   Turner
Katherine   Green
Douglas Perry
Kenneth Carter
David   King
Debra   Johnson
Bruce   Ross
Victor  Lewis
Louis   Young

The script might crash at some random point within the file after it starts reading from the top of the file to the bottom (Note: this is not the issue). I can know exactly at what name the script failed and save it, but regardless would like to iterate my script through every name. Hence, if the script runs through the whole file, there is no issue and no name is saved, but if it is not the EOF, the name that was being processed as the script fail should be saved.

The function I am using to process the names is called by giving it a list with all the names, and is as follows:

def processNames(names):
    for name in names:
        try:
            processing(name)
        except:
            print('Failed. Will try in next script run')
            break

How can I use the name at which the script failed, and run the next iteration starting from the name at which it previously failed?

KaanTheGuru
  • 373
  • 1
  • 2
  • 11

1 Answers1

1

You can do it with an additional counter variable to record where it crashed. Code:

def processNames(names):
    counter=0
    for index, name in enumerate(names):
        if index>counter:
            try:
                processing(name)
            except:
                print('Failed. Will try in next script run')
                counter=index

Reflecting on the comments this might be better:

def processNames(names, counter):
    for index, name in enumerate(names):
        if index>counter:
            try:
                processing(name)
            except:
                print('Failed. Will try in next script run')
                counter=index
    return whateveryouneed, counter

Also might want to look up for-else loop.

zabop
  • 6,750
  • 3
  • 39
  • 84
  • You need to save `counter` somewhere outside the function, so it's not reset to zero when the function is called again. – user200783 Feb 22 '19 at 13:01
  • 1
    @zabop that does the job. Thank you! – KaanTheGuru Feb 22 '19 at 13:03
  • 1
    This will set up a `counter` to the *last* failing name index. If you need one by one - you need to add a `break`. Also - counter is neither returned nor visible outside of the function. – phoenix Feb 22 '19 at 13:04
  • @phoenix is correct about `break`. Also, you probably want `index >= counter` to retry the failed name rather than skip over it. – user200783 Feb 22 '19 at 13:06