Novark
Novark

Reputation: 419

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

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.

Upvotes: 3

Views: 2743

Answers (2)

Jason
Jason

Reputation: 3917

There isn't a way to only retrieve directories from the operating system. You have to filter the results. Although, it looks like using os.scandir improves performance by an order of magnitude (see benchmarks) over os.listdir and the older os.walk implementation since it avoids retrieving anything but metadata where possible. If you're using 3.5, it's already integrated into the standard library. Otherwise, it looks like you need to use the scandir package.

To filter the results from os.scandir

ds = [e.name() for e in os.scandir('.') if e.is_dir()]

According to the documentation, walk is implemented in terms of scandir which also gives the same speedup.

Upvotes: 1

Anand S Kumar
Anand S Kumar

Reputation: 90899

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.

Upvotes: 2

Related Questions