Mercurial > hg > config
annotate python/walk.py @ 876:c4044724689a
addtl instructions
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 08 May 2019 20:34:07 -0700 |
parents | 0afeb265da7f |
children |
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 | 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 | 12 |
13 def ensure_dir(path): | |
14 """ensures `path` is a directory""" | |
15 return os.path.isdir(path) | |
16 | |
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 | 28 |
29 # parse command line | |
30 | |
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 | 34 |
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 | 43 sys.stderr.write("'%s' not a file or directory\n" % arg) |
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() |