Skip to content

Instantly share code, notes, and snippets.

@KunoiSayami
Created April 7, 2020 13:30
Show Gist options
  • Save KunoiSayami/f875c8d7a7e4e1842304bde8cb9cf193 to your computer and use it in GitHub Desktop.
Save KunoiSayami/f875c8d7a7e4e1842304bde8cb9cf193 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import subprocess
import os
from typing import NoReturn
def walksdir(_dir: str='.') -> NoReturn:
for root, dirs, _ in os.walk(_dir):
#print(dirs)
for x in dirs:
subp = subprocess.Popen(['du', '-sh', x], stdout=subprocess.PIPE)
subp.wait()
yield subp.communicate()[0].decode()
break
def walksfile(_dir: str='.') -> NoReturn:
for root, _, files in os.walk(_dir):
for x in files:
subp = subprocess.Popen(['ls', '-lh', x], stdout=subprocess.PIPE)
subp.wait()
yield subp.communicate()[0].decode()
break # list current directory file only
def convert(size: str) -> float:
base = 1
if size[-1] == 'M':
base = 1024
elif size[-1] == 'G':
base = 1024 * 1024
if len(size) == 1: size = '10'
return float(size[:-1]) * base
dirstr = []
for x in walksdir():
dirstr.append(x)
for x in walksfile():
s = x.split()
#print(s)
dirstr.append('\t'.join((s[4], ' '.join(s[8:])))+'\n')
for x in sorted(dirstr, key=lambda x: convert(x.split()[0])):
print(x, end='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment