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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:33:25+00:00 2026-06-09T00:33:25+00:00

I have a (recursive) function which I would like to accept either a string

  • 0

I have a (recursive) function which I would like to accept either a string or an opened file object. If the argument is a string, then the function opens a file and uses that file object. It seems best to close this opened file object explicitly when I return from the function, but only if a string was passed in. (Imagine the surprise from the user when they pass in an opened file object and find that their file object was closed somewhere). Here’s what I’m currently using:

def read_file(f, param):
    do_close = isinstance(f,basestring)
    f = open(f, 'rb') if do_close else f
    try:
        info = f.read(4)
        #check info here
        if info == Info_I_Want(param):
           return f.read(get_data(info))
        else:
           f.seek(goto_new_position(info))
           return read_file(f,param)
    except IKnowThisError:
           return None
    finally:
        if do_close:
           f.close()

You can assume that IKnowThisError will be raised at some point if I don’t find the info I want.

This feels very kludgy. Is there a better way?

  • 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-09T00:33:26+00:00Added an answer on June 9, 2026 at 12:33 am

    Why not wrapping your recursive function with a wrapper to avoid overhead ?

    def read_file(f, param):
        if isinstance(f, basestring):
            with open(f, 'rb') as real_f:
                return read_file2(real_f, param)
        else:
            return read_file2(real_f, param)
    
    def read_file2(f, param):
        # Now f should be a file object
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a recursive function fact , which can be called from either an
I have a recursive function which does a sort of tree process where each
I have some sort of recursive function, but I need to parse a string,
I am creating a recursive js function which basically parses a string. I do
I have a recursive function working on a list, the function contains a loop
I have a recursive function reading a table of contents of documents from a
Lets say I have a recursive function that creates lists within lists. It return
I am currently trying to build a program where I have a recursive function
I have a simple recursive function RCompare() that calls a more complex function Compare()
After some recursive function I have an array: first iteration: Array ( [category_id] =>

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.