VickiT05
VickiT05

Reputation: 101

python Search for subdirectories

I'd like to create a list of all sub-directories that contain "HICU-B" in their name. Only directories not files (some files in the parent directory contain this too).

I'm wondering if there is a way to combine glob and os.walk() together to accomplish this. Or if there is another way to go about it.

This is the code I have so far:

This gets the files and directories that contain the text I would like.

dirstext=glob.glob('/data01/HICU-B*')

And this gets the directories.

dirs=next(os.walk('/data01'))[1]

I can't figure out how to combine them to get just the directories with "HICU-B" in the name and not the files.

Thoughts?

Upvotes: 0

Views: 61

Answers (1)

Moses Koledoye
Moses Koledoye

Reputation: 78564

Append a slash '/' to the end of HICU-B*:

dirstext = glob.glob('/data01/HICU-B*/')

This would return only directories with names starting with HICU-B* while files will be excluded.

If you need to walk through the matching directories to return all their subdirectories and files, you can do:

dirs = [list(i) for i in map(os.walk, dirstext)]

Upvotes: 2

Related Questions