annotate python/walk.py @ 922:74dc16f074be default tip

ignore ff merges
author Jeff Hammel <k0scist@gmail.com>
date Thu, 03 Oct 2024 05:36:19 -0700
parents 0afeb265da7f
children
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
810
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
7 # imports
809
b3f75f1361c5 minor mostly formatting fixes
Jeff Hammel <k0scist@gmail.com>
parents: 160
diff changeset
8 import argparse
160
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import os
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import sys
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11
810
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
12
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
13 def ensure_dir(path):
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
14 """ensures `path` is a directory"""
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
15 return os.path.isdir(path)
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
16
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
17
160
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 def all_files(directory):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 filenames = []
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 for dirpath, dirnames, files in os.walk('/home/jhammel/music'):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 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
22 return sorted(filenames)
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23
809
b3f75f1361c5 minor mostly formatting fixes
Jeff Hammel <k0scist@gmail.com>
parents: 160
diff changeset
24
160
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 def main(args=sys.argv[1:]):
809
b3f75f1361c5 minor mostly formatting fixes
Jeff Hammel <k0scist@gmail.com>
parents: 160
diff changeset
26 """CLI"""
b3f75f1361c5 minor mostly formatting fixes
Jeff Hammel <k0scist@gmail.com>
parents: 160
diff changeset
27
810
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
28
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
29 # parse command line
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
30
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
31 # sanity
160
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 if not args:
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 print "Usage: %s directory [directory] [...]" % os.path.basename(sys.argv[0])
810
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
34
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
35 # process command line
160
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 for arg in args:
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 if os.path.isdir(arg):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 for i in all_files(arg):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 print i
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 elif os.path.isfile(arg):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 print os.path.abspath(arg)
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 else:
810
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
43 sys.stderr.write("'%s' not a file or directory\n" % arg)
0afeb265da7f fix formatting bug
Jeff Hammel <k0scist@gmail.com>
parents: 809
diff changeset
44
809
b3f75f1361c5 minor mostly formatting fixes
Jeff Hammel <k0scist@gmail.com>
parents: 160
diff changeset
45
160
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 if __name__ == '__main__':
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 main()