Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to retrieve subdirectories in Python without having to iterate over all files?

Tags:

python

os.walk

I'm looking for a way to list the sub-directories contained with the current working directory, however, I haven't been able to find a way that doesn't iterate over all files.

Essentially, if I have a folder with a large number of files and 2 folders, I want a method that can quickly return a list containing the names of the 2 folders without having to scan all of the files, too.

Is there a way to do this in Python?

Edit: I should clarify, that my question is in regards to the performance of retrieving the directories. I already know of several ways to get the directories, but they're all slowed down if the working directory has a bunch of files in it as well.

like image 955
Novark Avatar asked Dec 12 '25 19:12

Novark


1 Answers

Not sure if there is any direct standard functions that would do this for you. But You can use os.walk() for this , each iteration of os.walk() returns a tuple of the format -

(dirpath, dirnames, filenames)

Where dirpath is the directory being walked currently, dirnames contains the directories inside dirpath and filenames contains the files inside it.

You can just directly call next(os.walk()) to get the above tuple result for a directory, then the second element (index - 1) in that tuple would be the sub-folders inside the directory.

Code -

direcs = next(os.walk('.'))[1]

direcs at the end would be a list of the subfolders of current folder. You can also give some other folder in there to get the list of folders inside it.

like image 198
Anand S Kumar Avatar answered Dec 14 '25 10:12

Anand S Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!