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