Skip to content

Instantly share code, notes, and snippets.

@Wingjam
Created August 19, 2017 19:22
Show Gist options
  • Save Wingjam/77e73e8aaf12b53bc91d2a343cf13fcb to your computer and use it in GitHub Desktop.
Save Wingjam/77e73e8aaf12b53bc91d2a343cf13fcb to your computer and use it in GitHub Desktop.
Print all files bigger than the size received in parameter
import argparse
import os
import math
parser = argparse.ArgumentParser(description="Print all files bigger than the size received in parameter")
parser.add_argument("-p", "--path", help="The path where to find files recursively", type=str, default=".")
parser.add_argument("-s", "--size", help="The size (in bytes) minimum to print files", type=int, default=0)
args = parser.parse_args()
def convert_size(size_bytes):
if size_bytes == 0:
return "0 B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return "{} {}".format(s, size_name[i])
big_files = {}
for root, dirs, files in os.walk(args.path):
for name in files:
file_path = os.path.join(root, name)
file_size = os.path.getsize(file_path)
if file_size > args.size:
big_files[file_path] = file_size
# Get a list of tuples (file_name, file_size) ordered by value (file_size)
big_files = sorted(big_files.items(), key=lambda x: x[1], reverse=True)
print("Files bigger than {} : ".format(convert_size(args.size)))
for file_name, file_size in big_files:
print("{:<10} {}".format(convert_size(file_size), file_name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment