Mercurial > hg > config
comparison python/walk.py @ 160:83928b2f2776
add file walking example code
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 01 Aug 2011 18:30:57 -0700 |
parents | |
children | b3f75f1361c5 |
comparison
equal
deleted
inserted
replaced
159:9b81f964c524 | 160:83928b2f2776 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import os | |
4 import sys | |
5 | |
6 def all_files(directory): | |
7 filenames = [] | |
8 for dirpath, dirnames, files in os.walk('/home/jhammel/music'): | |
9 filenames.extend([os.path.join(dirpath, f) for f in files]) | |
10 return sorted(filenames) | |
11 | |
12 def main(args=sys.argv[1:]): | |
13 if not args: | |
14 print "Usage: %s directory [directory] [...]" % os.path.basename(sys.argv[0]) | |
15 for arg in args: | |
16 if os.path.isdir(arg): | |
17 for i in all_files(arg): | |
18 print i | |
19 elif os.path.isfile(arg): | |
20 print os.path.abspath(arg) | |
21 else: | |
22 print >> sys.stderr, "'%s' not a file or directory" | |
23 | |
24 if __name__ == '__main__': | |
25 main() |