From e13e85ec8efe1559b07193610a250836e031a8a2 Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Sun, 3 Apr 2011 13:55:59 -0700 Subject: [PATCH] pickrand.py: improvements Don't set followlinks (it doesn't work on old versions of Python). Add -S argument meaning "also echo to stderr" Complain about unknown command-line arguments. Signed-off-by: Colin McCabe --- pickrand.py | 13 ++++++++++++- 1 files changed, 12 insertions(+), 1 deletions(-) diff --git a/pickrand.py b/pickrand.py index 9efec59..558115c 100755 --- a/pickrand.py +++ b/pickrand.py @@ -4,9 +4,18 @@ import os import random import sys +print_to_stderr = False +if (len(sys.argv) == 1): + pass +elif (len(sys.argv) == 2) and (sys.argv[1] == "-S"): + print_to_stderr = True +else: + print >>sys.stderr, "invalid command-line arguments" + sys.exit(1) + allfiles = [] -for root, dirs, files in os.walk(".", followlinks=True): +for root, dirs, files in os.walk("."): for f in files: allfiles.append(os.path.join(root, f)) if (len(allfiles) == 0): @@ -14,4 +23,6 @@ if (len(allfiles) == 0): random.seed(None) r = random.randint(0,len(allfiles) - 1) print(allfiles[r]) +if (print_to_stderr): + print >>sys.stderr, (allfiles[r]) sys.exit(0) -- 1.6.6.rc1.39.g9a42