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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:23:26+00:00 2026-05-20T20:23:26+00:00

What I need to do is loop over a large number of different files

  • 0

What I need to do is loop over a large number of different files and (try to) fetch metadata from the files.

I can make a large if…elif… and test for every extension, but I think it would be much easier to store the extension in a variable, check if a function with that name exists, and execute it.

This is my current solution, taken from another stackoverflow thread:

try:
    getattr(modulename, funcname)(arg)
except AttributeError:
    print 'function not found "%s" (%s)' % (funcname, arg)

There is a problem with this:
If the underlying function raises an AttributeError, this is registered as a “function not found” error.
I can add try…except blocks to all functions, but that would not be particularly pretty either …

What I’m looking for is more something like:

if function_exists(fun):
  execute_function(fun, arg)

Is there a straightforward way of doing this?

Thanks 🙂

  • 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-05-20T20:23:27+00:00Added an answer on May 20, 2026 at 8:23 pm

    You can do :

    func = getattr(modulename, funcname, None):
    if func:
        func(arg)
    

    Or maybe better:

    try:
        func = getattr(modulename, funcname)
    except AttributeError:
        print 'function not found "%s" (%s)' % (funcname, arg)
    else:
        func(arg)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number rather large, complex xml documents that I need to loop
I need to have very high-performance loop going over large datasets. I need to
If anybody can help me to write a loop to iterate over files in
I need to loop over a query exactly 12 times to complete rows in
I need to be able to loop over only a section at a time
I need to negate very large number of doubles quickly. If bit_generator generates 0,
I need to loop over my array and set all the vars to false
I am writing a PHP function that will need to loop over an array
I'm writing a program which will need to do a very large number of
For purposes of testing compression, I need to be able to create large files,

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.