#NEWLINE# def walklevels( path , levels = 1 ) : #NEWLINE# #INDENT# 'Wrap os.walk to allow setting recursion depth.' #NEWLINE# path = path . rstrip ( os . path . sep ) #NEWLINE# assert os . path . isdir ( path ) #NEWLINE# num_sep = path . count ( os . path . sep ) #NEWLINE# for ( root , dirs , files ) in os . walk ( path ) : #NEWLINE# #INDENT# ( yield ( root , dirs , num_sep ) ) #NEWLINE# num_sep_this = root . count ( os . path . sep ) #NEWLINE# if ( ( num_sep + levels ) <= num_sep_this ) : #NEWLINE# #INDENT# del dirs [ : ]