File: tagpix/examples/ls2.py
#!/usr/bin/env python3 """ ============================================================================== Simple custom recursive ls: shows structure and links, with improved support for hidden files, and platform-neutral sort order. '-a' shows any ".*" too (with "_" and space so "." visible), and folder items are always sorted as case-significant+ascending. Run as "ls2.py <folder> [-a]" where <folder> is the root of the tree to be listed: /somedir$ /Code/ls2.py test1 -a ___ test1 ______ .DS_Store ______ dirlink => dir ______ file ______ filelink => file ______ nestedfilelink => dir/nestedfile ______ test1/dir _________ .other _________ nestedfile The output is substantially more readable than a Unix "ls -aR" equivalent. ============================================================================== """ from __future__ import print_function # use py 3.X or 2.X import os, sys all = '-a' in sys.argv[2:] # past script, folder unixhiddens = ('.', '._') # ignore if not -a ichar = '_' level = 0 for (here, subs, files) in os.walk(sys.argv[1]): indent = len(here.split(os.sep)) print(ichar * (indent*3), here) for s in sorted(subs): if not all and s.startswith(unixhiddens): continue sp = os.path.join(here, s) if os.path.islink(sp): print(ichar * ((indent+1)*3), s, '=>', os.readlink(sp)) for f in sorted(files): if not all and f.startswith(unixhiddens): continue fp = os.path.join(here, f) if os.path.islink(fp): print(ichar * ((indent+1)*3), f, '=>', os.readlink(fp)) else: print(ichar * ((indent+1)*3), f)