list - Traverse directories to a specific depth in Python -


this question has answer here:

i search , print directories under c:// example, list 1st , 2nd levels down, contain sp30070156-1.

what efficient way using python 2 without script running though entire sub-directories (so many in case take long time)

typical directory names follow:

rooty hill sp30068539-1 3rd split unit ac project oxford falls sp30064418-1 upgrade ses msb queanbeyan sp30066062-1 ac 

you can try create function based on os.walk(). should started:

import os  def walker(base_dir, level=1, string=none):     results = []     root, dirs, files in os.walk(base_dir):         _root = root.replace(base_dir + '\\', '') #you may need remove "+ '\\'"     if _root.count('\\') < level:         if string none:             results.append(dirs)         else:             if string in dirs:                results.append(dirs)     return results 

then can call string='sp30070156-1' , level 1 level 2.

not sure if it's going faster 40s, though.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -