user1895915
user1895915

Reputation: 89

Print subfolder without parent folder?

I am trying to count the files subfolder from a folder but when trying to see what the path returns, i see that it includes also the path to the parent folder and not only the subfolder paths only. how to i exclude the parent folder from my output? Here is my code

for path, dirs, files in os.walk('data'):
    print path

Here is the output.

data
data/02062016
data/03062016
data/07022016
data/11252016
data/12042015

I don't want the data parent folder included which will only return.

data/02062016
data/03062016
data/07022016
data/11252016
data/12042015

Upvotes: 0

Views: 665

Answers (2)

Wayne Werner
Wayne Werner

Reputation: 51807

That's because path is where you currently are. If you just want to print the subdirectories you'll need to do something like this:

for path, dirs, files in os.walk('data'):
    for dirname in dirs:
        print(os.path.join(path, dirname))

Upvotes: 1

Neil
Neil

Reputation: 81

Use os.listdir('data')

This will prevent entering subdirectories (like walk does) fyi. And also list subdirectories.

Upvotes: 0

Related Questions