comparison python/walk.py @ 809:b3f75f1361c5

minor mostly formatting fixes
author Jeff Hammel <k0scist@gmail.com>
date Fri, 28 Oct 2016 17:27:32 -0700
parents 83928b2f2776
children 0afeb265da7f
comparison
equal deleted inserted replaced
808:48ea50d346de 809:b3f75f1361c5
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 """
4 illustration of walking a directory structure
5 """
6
7 import argparse
3 import os 8 import os
4 import sys 9 import sys
5 10
6 def all_files(directory): 11 def all_files(directory):
7 filenames = [] 12 filenames = []
8 for dirpath, dirnames, files in os.walk('/home/jhammel/music'): 13 for dirpath, dirnames, files in os.walk('/home/jhammel/music'):
9 filenames.extend([os.path.join(dirpath, f) for f in files]) 14 filenames.extend([os.path.join(dirpath, f) for f in files])
10 return sorted(filenames) 15 return sorted(filenames)
11 16
17
12 def main(args=sys.argv[1:]): 18 def main(args=sys.argv[1:]):
19 """CLI"""
20
13 if not args: 21 if not args:
14 print "Usage: %s directory [directory] [...]" % os.path.basename(sys.argv[0]) 22 print "Usage: %s directory [directory] [...]" % os.path.basename(sys.argv[0])
15 for arg in args: 23 for arg in args:
16 if os.path.isdir(arg): 24 if os.path.isdir(arg):
17 for i in all_files(arg): 25 for i in all_files(arg):
18 print i 26 print i
19 elif os.path.isfile(arg): 27 elif os.path.isfile(arg):
20 print os.path.abspath(arg) 28 print os.path.abspath(arg)
21 else: 29 else:
22 print >> sys.stderr, "'%s' not a file or directory" 30 print >> sys.stderr, "'%s' not a file or directory"
23 31
24 if __name__ == '__main__': 32 if __name__ == '__main__':
25 main() 33 main()