changeset 160:83928b2f2776

add file walking example code
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 01 Aug 2011 18:30:57 -0700
parents 9b81f964c524
children 2de4b1d92c1c
files python/walk.py
diffstat 1 files changed, 25 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/walk.py	Mon Aug 01 18:30:57 2011 -0700
@@ -0,0 +1,25 @@
+#!/usr/bin/env python
+
+import os
+import sys
+
+def all_files(directory):
+    filenames = []
+    for dirpath, dirnames, files in os.walk('/home/jhammel/music'):
+        filenames.extend([os.path.join(dirpath, f) for f in files])
+    return sorted(filenames)
+
+def main(args=sys.argv[1:]):
+    if not args:
+        print "Usage: %s directory [directory] [...]" % os.path.basename(sys.argv[0])
+    for arg in args:
+        if os.path.isdir(arg):
+            for i in all_files(arg):
+                print i
+        elif os.path.isfile(arg):
+            print os.path.abspath(arg)
+        else:
+            print >> sys.stderr, "'%s' not a file or directory"
+        
+if __name__ == '__main__':
+    main()