Okay Lolz
Okay Lolz

Reputation: 423

How to skip AttributeError in for loop python and continue the loop

I using a 'for loop' to record the IMEI number from multiple files in an array.

When, but when no IMEI is detected the for loop stops indicating AttributeError:rint.

    result = getattr(asarray(obj), method)(*args, **kwds)
    AttributeError: rint

I have used this method:

for IMEI in file():
    try:
        detect the IMEI from the files()
        Append them to the array()
    exception AttributeError:
        print 'File name'
        pass

What I would like to do is to skip the error if the IMEI is not detected in the file and then continue with the loop looking for IMEI in other files.

IMEI refers to 16Digit AlphaNumeric code. I use it as a 'string'.

There are 3200 '.dat' files which I processed for finding such a Alpha Numeric text in each file. Each dat file has some HEX data in it.

Upvotes: 1

Views: 8015

Answers (2)

Okay Lolz
Okay Lolz

Reputation: 423

I found the answer, which works in my case.

Answer:

if IMEI is None:
    continue

This helps in skipping the loop if the return value is None and as in my case the AttributeError meant the same.

Upvotes: 2

Shrey
Shrey

Reputation: 1260

try:
    execute_something()
except AttributeError:
    caught_error()

This is how we catch an error in python. Just add the above code snippet in your for loop. Replace the functions i used with your own code.

Please ask more if something is still wrong.

Upvotes: 3

Related Questions