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

  • Home
  • SEARCH
  • 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 8280741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:47:29+00:00 2026-06-08T09:47:29+00:00

Problem: I have code that looks for a file and open it. By default

  • 0

Problem:

I have code that looks for a file and open it. By default it looks for file that starts with ####### (each # being a number).

Problem is sometimes the file name is ##-##### and other times #####.

I would like a way if the file cannot be found try looking for the other two ways the file could be written.

An IOError exception happens when the file is not found. What I was thinking was to have an except statement that says:

except File2:
    Look for ##### in  myfindFileFunction()
    if file is still not found run except File3
except File3:
    Look for ##-#### in myfindFileFuction()
except:
    print "File not found"

What I am not sure of is how to set up custom exception to work this way, and/or if there is a more pythonic way to do this altogether…

Would setting up a pattern or the three possible file names and iterate thought each until the file is found work better?

  • 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-08T09:47:31+00:00Added an answer on June 8, 2026 at 9:47 am

    Using try/except is indeed a very pythonic (and fast) way of doing things.

    You have to weigh not only if it’s pythonic, but what impact does that approach has in terms of readability. Will you still understand the code quickly when you look at it again in 6 months? Will somebody else?

    I usually make sure that slightly complex try/except clauses to handle this kind of things are well commented. Asides from that… it’s a perfectly reasonable way of doing it.

    Also, to put your mind at ease regarding performance, a common concern when one is deciding between two approaches, take a look here: Python if vs try-except and you’ll see that try/except constructs are fast in Python… really fast.

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

Sidebar

Related Questions

I have a CSS file that looks fine when I open it using gedit
I have a problem that has been driving me batty. I have code that
I have some code that I'd like to run on a page. My problem
my first question here! I have a problem with a piece of code that
I have a dynamic list code that works just fine. My only problem now
I have a code problem which stems from the fact that I am using
i have the following problem with that code in my html tag: <div id=searchbar><input
Hi I have a code example available here: http://jsbin.com/oxoweh My problem is that I
I recently have a problem that my java code works perfectly ok on my
So I have the following code, the problem is that it exits before all

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.