X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=pickrand.py;h=688460a136ded9b55492e438b71f0898bd3c6b9d;hb=2d3ef1c886d1832709dcaec12ed6b6b06189b9fc;hp=558115c3f4b1a5841eb4990f7cc2ef0e4ef5757e;hpb=e13e85ec8efe1559b07193610a250836e031a8a2;p=cmccabe-bin diff --git a/pickrand.py b/pickrand.py index 558115c..688460a 100755 --- a/pickrand.py +++ b/pickrand.py @@ -3,26 +3,29 @@ import os import random import sys +import time print_to_stderr = False +random.seed(os.getpid() + int(time.time())) + +file_name = None if (len(sys.argv) == 1): - pass + 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: - print >>sys.stderr, "invalid command-line arguments" - sys.exit(1) - -allfiles = [] + allfiles = sys.argv[1:] + r = random.randint(0,len(allfiles) - 1) + file_name = allfiles[r] -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) -random.seed(None) -r = random.randint(0,len(allfiles) - 1) -print(allfiles[r]) +print(file_name) if (print_to_stderr): - print >>sys.stderr, (allfiles[r]) + print >>sys.stderr, file_name sys.exit(0)