# HG changeset patch # User Jeff Hammel # Date 1477701215 25200 # Node ID 0afeb265da7f24c5be821c6e36f8d2339d8dbdc4 # Parent b3f75f1361c521ebe4fdac39621107366112fb46 fix formatting bug diff -r b3f75f1361c5 -r 0afeb265da7f python/walk.py --- a/python/walk.py Fri Oct 28 17:27:32 2016 -0700 +++ b/python/walk.py Fri Oct 28 17:33:35 2016 -0700 @@ -4,10 +4,17 @@ illustration of walking a directory structure """ +# imports import argparse import os import sys + +def ensure_dir(path): + """ensures `path` is a directory""" + return os.path.isdir(path) + + def all_files(directory): filenames = [] for dirpath, dirnames, files in os.walk('/home/jhammel/music'): @@ -18,8 +25,14 @@ def main(args=sys.argv[1:]): """CLI""" + + # parse command line + + # sanity if not args: print "Usage: %s directory [directory] [...]" % os.path.basename(sys.argv[0]) + + # process command line for arg in args: if os.path.isdir(arg): for i in all_files(arg): @@ -27,7 +40,8 @@ elif os.path.isfile(arg): print os.path.abspath(arg) else: - print >> sys.stderr, "'%s' not a file or directory" + sys.stderr.write("'%s' not a file or directory\n" % arg) + if __name__ == '__main__': main()