Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import os

subDir = []
def listdirs(path, count, which_dir):
    for roots, dirs, files in os.walk(path):
        for dir in dirs:
            d = os.path.join(roots, dir)
            subDir.append(d)
    b = [s.split() for s in subDir]
    return b[which_dir]

rootdir = '/home/runner/TestP1'
count = 0
directory = listdirs(rootdir, count, 1)
which = len(subDir)

print(f"Total directories: {which}")
print(f"Directory: {directory}")


What I have tried:

In this code, I want to set the directory = listdirs(rootdir, count, 1) in for loop so that I could print all the paths and use them further one by one. But the problem is that I wrote which variable above the listdirs() function like this,

rootdir = '/home/runner/TestP1'
count = 0
which = len(subDir)
print(f"Total directories: {which}")
for x in range(which):
    directory = listdirs(rootdir, count, x)
    print(f"Directory: {directory}")

But it gave me this result

Total directories: 0


It didn't show me the a directory
Posted
Updated 16-Mar-21 4:42am
v2
Comments
Richard MacCutchan 16-Mar-21 10:35am    
This is the third time you have posted this question and it is still unclear, a) what result you are trying to get, and b) what actual problem you are seeing.
ibilalkayy 16-Mar-21 10:38am    
Please read the description I have written that a) what result I am trying to get and b) what is the output I am seeing

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