X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=pickrand.py;h=6340971a6036e7bae1770e5901563926c65655bd;hb=9374e89eef8115bf1680585eb32ab11bb1942525;hp=8253e74a1f6c50102aae0bc2cafb73a0b706e16d;hpb=17f7ddbe7d6b37982cf03c10330f66cff9b355cc;p=cmccabe-bin diff --git a/pickrand.py b/pickrand.py index 8253e74..6340971 100755 --- a/pickrand.py +++ b/pickrand.py @@ -4,14 +4,25 @@ 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): sys.exit(1) -random.seed(None) -r = random.randint(0,len(allfiles)) +random.seed(os.getpid()) +r = random.randint(0,len(allfiles) - 1) print(allfiles[r]) +if (print_to_stderr): + print >>sys.stderr, (allfiles[r]) sys.exit(0)