Mercurial > hg > config
comparison python/walk.py @ 810:0afeb265da7f
fix formatting bug
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 28 Oct 2016 17:33:35 -0700 |
parents | b3f75f1361c5 |
children |
comparison
equal
deleted
inserted
replaced
809:b3f75f1361c5 | 810:0afeb265da7f |
---|---|
2 | 2 |
3 """ | 3 """ |
4 illustration of walking a directory structure | 4 illustration of walking a directory structure |
5 """ | 5 """ |
6 | 6 |
7 # imports | |
7 import argparse | 8 import argparse |
8 import os | 9 import os |
9 import sys | 10 import sys |
11 | |
12 | |
13 def ensure_dir(path): | |
14 """ensures `path` is a directory""" | |
15 return os.path.isdir(path) | |
16 | |
10 | 17 |
11 def all_files(directory): | 18 def all_files(directory): |
12 filenames = [] | 19 filenames = [] |
13 for dirpath, dirnames, files in os.walk('/home/jhammel/music'): | 20 for dirpath, dirnames, files in os.walk('/home/jhammel/music'): |
14 filenames.extend([os.path.join(dirpath, f) for f in files]) | 21 filenames.extend([os.path.join(dirpath, f) for f in files]) |
16 | 23 |
17 | 24 |
18 def main(args=sys.argv[1:]): | 25 def main(args=sys.argv[1:]): |
19 """CLI""" | 26 """CLI""" |
20 | 27 |
28 | |
29 # parse command line | |
30 | |
31 # sanity | |
21 if not args: | 32 if not args: |
22 print "Usage: %s directory [directory] [...]" % os.path.basename(sys.argv[0]) | 33 print "Usage: %s directory [directory] [...]" % os.path.basename(sys.argv[0]) |
34 | |
35 # process command line | |
23 for arg in args: | 36 for arg in args: |
24 if os.path.isdir(arg): | 37 if os.path.isdir(arg): |
25 for i in all_files(arg): | 38 for i in all_files(arg): |
26 print i | 39 print i |
27 elif os.path.isfile(arg): | 40 elif os.path.isfile(arg): |
28 print os.path.abspath(arg) | 41 print os.path.abspath(arg) |
29 else: | 42 else: |
30 print >> sys.stderr, "'%s' not a file or directory" | 43 sys.stderr.write("'%s' not a file or directory\n" % arg) |
44 | |
31 | 45 |
32 if __name__ == '__main__': | 46 if __name__ == '__main__': |
33 main() | 47 main() |