In [1]:
input_ = '''dir/file
dir/dir2/file2
dir/file3
dir2/alpha/beta/gamma/delta
dir2/alpha/beta/gamma/delta/
dir3/file4
dir3/file5'''
In [2]:
import pandas as pd
In [9]:
in1 = input_.split("\n")
in2 = [x.split("/") for x in in1]
In [10]:
in2
Out[10]:
In [12]:
df = pd.DataFrame.from_dict( in2 )
In [24]:
df.sort()
Out[24]:
In [111]:
def print_branches(thisdf,tab_level=0,tab_width=2,max_tab_level=6):
max_tab_level = thisdf.shape[1]-1 # number of columns - 1
if tab_level >= max_tab_level: return # sanity check
# grab top level directories
top_level = thisdf[tab_level].unique()
for branch in top_level:
if len( thisdf.apply(str) != "None" ) <= 0:
return # if directory is empty, otherwise recurse
# end if
# print directory content
if str(branch) != "None":
print "".join([" "]*tab_width*tab_level),branch
# end if
# recurse
try:
mydf = thisdf[thisdf[tab_level]==branch]
except KeyError:
return
# end try
print_branches(mydf,tab_level+1)
# end for branch
# end def print_branches
In [112]:
print_branches(df)