annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
160
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
809
b3f75f1361c5 minor mostly formatting fixes
Jeff Hammel <k0scist@gmail.com>
parents: 160
diff changeset
3 """
b3f75f1361c5 minor mostly formatting fixes
Jeff Hammel <k0scist@gmail.com>
parents: 160
diff changeset
4 illustration of walking a directory structure
b3f75f1361c5 minor mostly formatting fixes
Jeff Hammel <k0scist@gmail.com>
parents: 160
diff changeset
5 """
b3f75f1361c5 minor mostly formatting fixes
Jeff Hammel <k0scist@gmail.com>
parents: 160
diff changeset
6
b3f75f1361c5 minor mostly formatting fixes
Jeff Hammel <k0scist@gmail.com>
parents: 160
diff changeset
7 import argparse
160
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import os
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import sys
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 def all_files(directory):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 filenames = []
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 for dirpath, dirnames, files in os.walk('/home/jhammel/music'):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 filenames.extend([os.path.join(dirpath, f) for f in files])
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 return sorted(filenames)
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
809
b3f75f1361c5 minor mostly formatting fixes
Jeff Hammel <k0scist@gmail.com>
parents: 160
diff changeset
17
160
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 def main(args=sys.argv[1:]):
809
b3f75f1361c5 minor mostly formatting fixes
Jeff Hammel <k0scist@gmail.com>
parents: 160
diff changeset
19 """CLI"""
b3f75f1361c5 minor mostly formatting fixes
Jeff Hammel <k0scist@gmail.com>
parents: 160
diff changeset
20
160
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 if not args:
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 print "Usage: %s directory [directory] [...]" % os.path.basename(sys.argv[0])
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 for arg in args:
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 if os.path.isdir(arg):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 for i in all_files(arg):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 print i
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 elif os.path.isfile(arg):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 print os.path.abspath(arg)
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 else:
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 print >> sys.stderr, "'%s' not a file or directory"
809
b3f75f1361c5 minor mostly formatting fixes
Jeff Hammel <k0scist@gmail.com>
parents: 160
diff changeset
31
160
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 if __name__ == '__main__':
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 main()