I have tried os.listdir()
but it shows all the files & directories. I've tried other solutions but they mostly require a loop and I am finding ways to do it without a loop.
Asked
Active
Viewed 37 times
-2
-
1Use glob: `glob('./*/')` – Amit Vikram Singh Apr 24 '21 at 12:22
-
2Does this answer your question? [Getting a list of all subdirectories in the current directory](https://stackoverflow.com/questions/973473/getting-a-list-of-all-subdirectories-in-the-current-directory) – Amit Vikram Singh Apr 24 '21 at 12:23
-
Why don't you want to use a loop? Why does "but it shows all the files & directories." not suit your needs? – Karl Knechtel Apr 24 '21 at 12:32
2 Answers
0
Works with me:
import os
os.mkdir('f')
os.mkdir(r'f/file1')
os.mkdir(r'f/file2')
os.mkdir(r'f/file3')
os.mkdir('f/file1/abc')
os.mkdir('f/file2/def')
os.mkdir('f/file3/ghi')
os.listdir('f')
OT:
['file2', 'file3', 'file1']
listdir with a file
os.listdir('f/file1')
OT:
['abc']

AdrianSK
- 55
- 5