简体   繁体   中英

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

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))

Use os.listdir('data')

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM