annotate python/walk.py @ 694:ebca6d85213a

File "/usr/lib/python3/dist-packages/IPython/config/__init__.py", line 16, in <module> from .application import * File "/usr/lib/python3/dist-packages/IPython/config/application.py", line 31, in <module> from IPython.config.configurable import SingletonConfigurable File "/usr/lib/python3/dist-packages/IPython/config/configurable.py", line 33, in <module> from IPython.utils.text import indent, wrap_paragraphs File "/usr/lib/python3/dist-packages/IPython/utils/text.py", line 28, in <module> from IPython.external.path import path File "/usr/lib/python3/dist-packages/IPython/external/path/__init__.py", line 2, in <module> from path import * File "/home/jhammel/python/path.py", line 25 print root(path) ^
author Jeff Hammel <k0scist@gmail.com>
date Wed, 09 Jul 2014 16:26:49 -0700
parents 83928b2f2776
children b3f75f1361c5
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
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 import os
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 import sys
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 def all_files(directory):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 filenames = []
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 for dirpath, dirnames, files in os.walk('/home/jhammel/music'):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 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
10 return sorted(filenames)
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 def main(args=sys.argv[1:]):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 if not args:
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 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
15 for arg in args:
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 if os.path.isdir(arg):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 for i in all_files(arg):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 print i
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 elif os.path.isfile(arg):
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 print os.path.abspath(arg)
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 else:
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 print >> sys.stderr, "'%s' not a file or directory"
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 if __name__ == '__main__':
83928b2f2776 add file walking example code
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 main()