Created
August 22, 2015 16:11
-
-
Save mrdrozdov/08950cd434b9625c5950 to your computer and use it in GitHub Desktop.
python walk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import argparse | |
""" | |
Example Usage: | |
# Basic | |
$ python walk.py ~/Downloads | |
# With file filter | |
$ python walk.py ~/Downloads -x .png | |
or | |
$ python walk.py ~/Downloads --extension .png | |
# Show only files | |
# - grep can be used to filter output of a process. | |
# - ^f is a regex that matches a string that begins with the letter f. | |
$ python walk.py ~/Downloads -x .png | grep ^f | |
""" | |
def do_stuff(root, extension=None): | |
for root, dirs, files in os.walk(root): | |
print "d: {d}".format(d=root) | |
for f in files: | |
_, ext = os.path.splitext(f) | |
if not extension or ext.lower() == extension.lower(): | |
print "f: {filename}".format(filename=os.path.join(root, f)) | |
if __name__ == '__main__': | |
print "begin walking" | |
parser = argparse.ArgumentParser() | |
parser.add_argument("root", help="Directory to walk.") | |
parser.add_argument("--extension", "-x", help="Filter by file extension. Include period like `.png` or `.txt`.") | |
args = parser.parse_args() | |
do_stuff(args.root, args.extension) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment