Skip to content

Instantly share code, notes, and snippets.

@MightyPork
Created July 22, 2015 22:01
Show Gist options
  • Save MightyPork/2a405285c1ddd9b5b7b4 to your computer and use it in GitHub Desktop.
Save MightyPork/2a405285c1ddd9b5b7b4 to your computer and use it in GitHub Desktop.
Los
#!/bin/env python3
import sys,os
import random
import functools
p_sg = ['el', 'la']
p_pl = ['los', 'las']
def with_prefix(name):
if name[-1:] == 's':
# plural
return random.choice(p_pl) + ' ' + name
else:
return random.choice(p_sg) + ' ' + name
# Find the path
path = '.'
if len(sys.argv) > 1:
for i, p in enumerate(sys.argv[1:]):
# ignore ls options
if p[0] == '-':
continue
path = p
# handle nonexistent
if not os.path.exists(path):
print('No such file or directory.', file=sys.stderr)
sys.exit(1)
# is not a directory
if not os.path.isdir(path):
print(with_prefix(os.path.basename(path)))
sys.exit(1)
files = os.listdir(path)
# comparator
def compare(x,y):
dirx = os.path.isdir(os.path.join(path, x))
diry = os.path.isdir(os.path.join(path, y))
if dirx and not diry: return -1
if diry and not dirx: return 1
if x < y: return -1
if x > y: return 1
return 0
# sort with comparator
files = os.listdir(path)
files.sort(key=functools.cmp_to_key(compare))
# print all the files
for f in files:
withp = with_prefix(f)
if os.path.isdir(os.path.join(path, f)):
withp += '/'
print(withp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment