Mercurial > hg > config
annotate python/walk.py @ 277:1096fddc22a3
new shit!!!
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 01 May 2013 06:56:38 -0700 |
parents | 83928b2f2776 |
children | b3f75f1361c5 |
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() |