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 8227307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:05:04+00:00 2026-06-07T16:05:04+00:00

So my problem is that my code would crash when one of it couldn’t

  • 0

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]))

  • 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-06-07T16:05:06+00:00Added an answer on June 7, 2026 at 4:05 pm

    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.

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

Sidebar

Related Questions

I would like to append the contents from one sqlite file (that has been
i have the following problem with that code in my html tag: <div id=searchbar><input
I have a problem that has been driving me batty. I have code that
My problem is that The code in the branch I'm working in for some
I recently have a problem that my java code works perfectly ok on my
I'm using PhantomJS page.evaluate() to do some scraping. My problem is that the code
I got this code today that works for jQuery v7. The problem is that
I have a bit of a problem that I can't seem to code my
I got the problem that the if-statement doesn't work. After the first code line
A minimal code that reproduces the problem is as follows: <div class=cell> <input type=text

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.