-2

So my problem is that my code would crash when one of it couldn't find the file in the server. Is there a way to skip that process of finding when there is no file found and continue through the loop. Here is my code below:

fname = '/Volumes/database/interpro/data/'+uniprotID+'.txt'

for index, (start, end) in enumerate(searchPFAM(fname)):
        with open('output_'+uniprotID+'-%s.txt' % index,'w') as fileinput:
            print start, end
            for item in lookup[uniprotID]:
                item, start, end = map(int, (item, start, end)) #make sure that all value is int
                if start <= item <= end:
                    print item
                    result = str(item - start)
                    fileinput.write(">{0} | at position {1} \n".format(uniprotID, result))
                    fileinput.write(''.join(makeList[start-1:end]))
                    break
            else:
                    fileinput.write(">{0} | N/A\n".format(uniprotID))
                    fileinput.write(''.join(makeList[start-1:end]))

Chad D
  • 499
  • 1
  • 10
  • 17

2 Answers2

11

You need to handle the exception using a try / except block. See the Python docs for handling exceptions.

In this case, you'd have to wrap the open() call (and everything in that with block), with try, and catch the exception with except IOError:

for ...
    try:
        with open(...
           # do stuff
    except IOError:
        # what to do if file not found, or pass

Additional info

What you really should do, is pull the body of that outer for loop out into a function. Or maybe the body of the with, into a function that handles the opened file. Either way, cutting down on the nesting makes things much more readable, and easier to make changes like this, adding a try/except.

Actually, it appears that you are re-opening the file every iteration of the outer for loop, but the filename never changes - you are always re-opening the same file. Is that intentional? If not, you probably want to re-think your logic, and move that outside the loop.

On third thought, what is the exception you're getting? Is it a file not found IOError? Because you're opening the file for writing ('w'), so I'm not sure why you would get that exception anyway.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
1
for index, (start, end) in enumerate(searchPFAM(fname)):
    try:
        newname = 'output_'+uniprotID+'-%s.txt' % index
        with open(newname,'w') as fileinput:
            print start, end
            for item in lookup[uniprotID]:
                item, start, end = map(int, (item, start, end)) #make sure that all value is int
                if start <= item <= end:
                    print item
                    result = str(item - start)
                    fileinput.write(">{0} | at position {1} \n".format(uniprotID, result))
                    fileinput.write(''.join(makeList[start-1:end]))
                    break
                else:
                    fileinput.write(">{0} | N/A\n".format(uniprotID))
                    fileinput.write(''.join(makeList[start-1:end]))
    except IOError:
        print 'Couldn't find file %s' % newname
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99