Skip to content

Instantly share code, notes, and snippets.

@almazkun
Created February 9, 2021 08:40
Show Gist options
  • Save almazkun/1fafd50eda89cd1affcfcae1e59655fb to your computer and use it in GitHub Desktop.
Save almazkun/1fafd50eda89cd1affcfcae1e59655fb to your computer and use it in GitHub Desktop.
# run the folder (recursively) finds all files of the provided extension
# or all files.
# Counts number of lines in each file.
# prints put the top 10 bigest files, by number of lines
# require python ver.: 3.9+
from pathlib import Path
def all_files(path=".", extention=None):
_ext = "**/*"
if extention is not None:
extention = extention.removeprefix(".")
_ext = f"**/*.{extention}"
for file in Path(path).glob(_ext):
if file.is_file():
yield file.resolve()
def top_longest(top=10, path=".", extention=None):
top_files = {}
for file in all_files(path=path, extention=extention):
with open(file, "r", errors='ignore') as f:
_len = len(f.readlines())
file_name = f.name
try:
top_files[_len].append(file_name)
except KeyError:
top_files[_len] = []
top_files[_len].append(file_name)
for _to in sorted(top_files.items(), reverse=True)[:top]:
print(_to)
if __name__ == "__main__":
top_longest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment