Last active
April 27, 2022 02:04
-
-
Save quincynyan/64a2eca376f0bb2827cbaa99d49942af to your computer and use it in GitHub Desktop.
Node Modules Sizer (Node Module file size scanner/calculator)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import os and shit | |
import os | |
# function to make total readable | |
def readable_size(size): | |
for x in ['bytes', 'KB', 'MB', 'GB', 'TB']: | |
if size < 1024.0: | |
return "%3.1f %s" % (size, x) | |
size /= 1024.0 | |
def get_size(start_path='.'): | |
total_size = 0 | |
for dirpath, dirnames, filenames in os.walk(start_path): | |
for f in filenames: | |
fp = os.path.join(dirpath, f) | |
# skip if it is symbolic link | |
if not os.path.islink(fp): | |
total_size += os.path.getsize(fp) | |
return total_size | |
def node_modules(path="C:"): | |
if path == "": | |
path = "C:" | |
total = 0 | |
# iterate os.walk for root | |
for root, dirs, files in os.walk(path): | |
# if its node_modules, calculate size | |
if root.endswith('node_modules'): | |
size = 0 | |
for file in dirs: | |
size += get_size(os.path.join(root, file)) | |
print('{} {}'.format(root, size)) | |
total += size | |
print("\n") | |
print('Total: {}'.format(readable_size(total))) | |
def main(): | |
print("Welcome to the Node Modules Sizer") | |
print("Enter path to scan: ") | |
path = input() | |
node_modules(path) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment