Click here to Skip to main content
15,917,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import os

subDir = []
def listdirs(rootdir, count):
    def recurse(rootdir):
        nonlocal count
        for file in os.listdir(rootdir):
            d = os.path.join(rootdir, file)
            if os.path.isdir(d):
                recurse(d)
                count += 1
                subDir.append(d)
    recurse(rootdir)
    print(f"Total directories are {count}")

rootdir = ('/home/runner/TestP1')
count = 0
listdirs(rootdir, count)
print(subDir[2])


What I have tried:

In this code, I am taking the rootdir, counting the subdirectories, and printing them by my choice. I used subDir to print the desired subdirectory. But I want to use the subDir as an argument. I tried it but failed.

How can I use subDir as an argument and print the desired result just like it is printing it without argument?
Posted
Updated 12-Mar-21 23:19pm
v2
Comments
Richard MacCutchan 13-Mar-21 5:19am    
What exactly do you mean by, "I want to use the subDir as an argument. I tried it but failed."?
ibilalkayy 13-Mar-21 9:39am    
I want to get the value of a specific subdirectory like subDir[2], from a root directory. I want to make an argument subDir in listdirs() function i.e. listdirs(rootdir, subDir[2], count) this should directly return me the value of subDir[2]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900