Skip to content

Instantly share code, notes, and snippets.

@nonZero
Created October 17, 2015 19:02
Show Gist options
  • Save nonZero/661128e11c40fb1bb5d5 to your computer and use it in GitHub Desktop.
Save nonZero/661128e11c40fb1bb5d5 to your computer and use it in GitHub Desktop.
Python 3.5 version
import collections
import pathlib
import sys
USAGE = """usage: {} path
displays number of files and total size per extension in the specified path."""
def get_info(path):
info = collections.defaultdict(lambda: {'count': 0, 'size': 0})
folder = pathlib.Path(path)
for f in folder.iterdir():
if not f.is_file():
continue
ext = f.suffix[1:] or '.'
info[ext]['count'] += 1
info[ext]['size'] += f.stat().st_size
return info
# print(get_info('.'))
# print(get_info('/usr/bin').items())
# print(get_info('/usr/share/pixmaps/').items())
if __name__ == "__main__":
if len(sys.argv) != 2:
print(USAGE.format(sys.argv[0]))
sys.exit(1)
info = get_info(sys.argv[1])
for ext, d in sorted(info.items()):
print(ext, d['count'], d['size'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment