Skip to content

Instantly share code, notes, and snippets.

@gicmo
Created February 17, 2017 16:48
Show Gist options
  • Save gicmo/031c5daa311705ff18d55e504e6f97ea to your computer and use it in GitHub Desktop.
Save gicmo/031c5daa311705ff18d55e504e6f97ea to your computer and use it in GitHub Desktop.
Count file extensions recursivly
#!/usr/bin/env python
# Do What the Fuck You Want to Public License
# for Olli with love, for his birthday
from __future__ import print_function
import argparse
import os
import os.path
import sys
def main():
parser = argparse.ArgumentParser(description="Count file extensions recursively.")
parser.add_argument("directory")
parser.add_argument("-N", type=int, default=20, dest='howmany')
parser.add_argument("--verbose", help="increase output verbosity",
action="store_true")
args = parser.parse_args()
print('Listening for %s' % args.directory)
cnt = {}
for root, directories, filenames in os.walk(args.directory):
for f in filenames:
if args.verbose:
print(f, file=sys.stderr)
name, ext = os.path.splitext(f)
if not len(ext):
print('ignoring %s' % name, file=sys.stderr)
continue
if ext not in cnt:
cnt[ext] = 1
else:
cnt[ext] += 1
print('Done scanning!', file=sys.stderr)
for ext, howmany in sorted(cnt.iteritems(), key=lambda x: x[1], reverse=True):
print('%s:\t%d' % (ext, howmany))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment