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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:21:05+00:00 2026-06-11T16:21:05+00:00

I have a function which is called recursively and I want to know the

  • 0

I have a function which is called recursively and I want to know the current level of recursion. Below code shows the method that I am using to calculate it, but it is not giving the expected results.

E.g. : To find the recursion level for a system path:

    import os
    funccount = 0

    def reccount(src):
        global funccount
        print "Function level of %s is %d" %(src, funccount)

    def runrec(src):
        global funccount
        funccount = funccount + 1
        lists = os.listdir(src)
        if((len(lists) == 0)):
            funccount = funccount - 1
        reccount(src)
        for x in lists:
             srcname = os.path.join(src, x)
             if((len(lists) - 1) == lists.index(x)):
                  if (not(os.path.isdir(srcname))):
                       funccount = funccount - 1
             if os.path.isdir(srcname):
                runrec(srcname)

    runrec(C:\test)

Problem : Given a directory path, print the level of recursion for directory

Directory Structure is :
In my directory structure, i will call the function “reccount(Test)” (Function will be called with path to MainFolder). I want to know the level of recursion call for each folder. (Directory only)

Test:
   |----------doc
   |----------share
                |----------doc
                            |----------file1
   |----------bin
                |----------common
                             |----------doc
   |----------extras
   |----------file2

When i call the procedure, i get the following result:

    Function level of C:\test is 1
    Function level of C:\test\bin is 2
    Function level of C:\test\bin\common is 3
    Function level of C:\test\bin\common\doc is 3
    Function level of C:\test\doc is 3
    Function level of C:\test\extras is 3
    Function level of C:\test\share is 4
    Function level of C:\test\share\doc is 5

As you can see, when it prints results for bin/common/doc, it prints 3 instead of 4 and all subsequent results are wrong

  • 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-11T16:21:06+00:00Added an answer on June 11, 2026 at 4:21 pm
    def some_method(data, level=0):
    
    
        some_method(..., level=level+1)
    
    
    if __name__ == '__main__':
        some_method(my_data)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code which is called at an onChange event of an function
So I have this code which is called in a print_info() function after a
I have a function which is called onmouseover that animates the element that was
hi i have function which is called by tinker listbox so i cannot return
I have a function which calculates distance from database records. This function is called
I am using jQuery(Latest release) and have a function called calculate() which gets called
I have a function, which is called sometimes with regular, sometimes dynamic arrays. If
I have a Function in CFC file, which will be called from .cfm file
I have a function: def create(sender, **kw): [...] Which should be called when the
I have a function called PreProcessSource, which allocates a boost::wave::context and does some preprocessing;

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.