# Collection of code snippets by Arne Vajhøj 
# (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
from os import listdir, stat
from sys import argv, exit
from time import strftime, localtime

if len(argv) < 4:
    print('Usage: python dir.py <dir> <orderby> <order>')
    exit(1)
sel = lambda f : f[0]
if argv[2].lower() == 'name':
    sel = lambda f : f[0]
if argv[2].lower() == 'size':
    sel = lambda f : f[1].st_size
if argv[2].lower() == 'time':
    sel = lambda f : f[1].st_mtime
rev = False
if argv[3].lower() == 'asc':
    rev = False
if argv[3].lower() == 'desc':
    rev = True

allf = [(f,stat(f)) for f in listdir(argv[1])]
allf.sort(reverse=rev, key=sel)
for f in allf:
    print('%-32s %10d %s' % (f[0], f[1].st_size, strftime('%d-%b-%Y %H:%M:%S', localtime(f[1].st_mtime))))
