X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=pickrand.py;h=688460a136ded9b55492e438b71f0898bd3c6b9d;hb=2d3ef1c886d1832709dcaec12ed6b6b06189b9fc;hp=8253e74a1f6c50102aae0bc2cafb73a0b706e16d;hpb=17f7ddbe7d6b37982cf03c10330f66cff9b355cc;p=cmccabe-bin diff --git a/pickrand.py b/pickrand.py index 8253e74..688460a 100755 --- a/pickrand.py +++ b/pickrand.py @@ -3,15 +3,29 @@ import os import random import sys +import time -allfiles = [] +print_to_stderr = False +random.seed(os.getpid() + int(time.time())) -for root, dirs, files in os.walk(".", followlinks=True): - for f in files: - allfiles.append(os.path.join(root, f)) -if (len(allfiles) == 0): - sys.exit(1) -random.seed(None) -r = random.randint(0,len(allfiles)) -print(allfiles[r]) +file_name = None +if (len(sys.argv) == 1): + allfiles = [] + for root, dirs, files in os.walk("."): + for f in files: + allfiles.append(os.path.join(root, f)) + if (len(allfiles) == 0): + sys.exit(1) + r = random.randint(0,len(allfiles) - 1) + file_name = allfiles[r] +elif (len(sys.argv) == 2) and (sys.argv[1] == "-S"): + print_to_stderr = True +else: + allfiles = sys.argv[1:] + r = random.randint(0,len(allfiles) - 1) + file_name = allfiles[r] + +print(file_name) +if (print_to_stderr): + print >>sys.stderr, file_name sys.exit(0)