Skip to content

Instantly share code, notes, and snippets.

@ilmoralito
Last active January 26, 2025 21:32
Show Gist options
  • Save ilmoralito/5ec5b47287124f8e7007d1e7644d7ca1 to your computer and use it in GitHub Desktop.
Save ilmoralito/5ec5b47287124f8e7007d1e7644d7ca1 to your computer and use it in GitHub Desktop.
This script counts and summarizes the file extensions of all files in the current working directory.
#!/usr/bin/env python3
import os
import pathlib
from operator import itemgetter
resume = {}
for filename in os.listdir():
if os.path.isfile(filename):
file_extension = pathlib.Path(filename).suffix or 'not extension'
if file_extension in resume:
resume[file_extension] += 1
else:
resume[file_extension] = 1
sorted_resume = dict(sorted(resume.items(), key=itemgetter(1), reverse=True))
for extension, count in sorted_resume.items():
print(f"{extension}: {count} files")
# run it with python3 extension_counter.py
# or add add execute permission to file by sudo chmod +x extension_counter.py then mv file to /usr/local/bin after that run the script with extension_counter.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment