@@ -68,19 +68,37 @@ def main(path: str, human: bool,
68
68
if must_ignore (root , ign ):
69
69
debug (f'ignore root { root } ' )
70
70
continue
71
+ files .sort (key = lambda f : f [0 ] == '.' )
71
72
for file in files :
72
73
fpath = os .path .join (root , file )
73
74
if must_ignore (fpath , ign ):
74
75
debug (f'ignore sub { fpath } ' )
75
76
continue
76
- size = os .path .getsize (fpath )
77
+ # size = os.path.getsize(fpath)
78
+ stat_info = os .stat (fpath )
79
+ size = stat_info .st_size
77
80
dirsz += size
78
81
total += size
79
- if root != path :
80
- print ( f' { size_to_str ( dirsz , human = human , digits = 1 ) } { root } ' )
81
- print ( f' { size_to_str ( total , human = human ) } { path } ' )
82
+ if root != path and dirsz > 0 :
83
+ print_line ( dirsz , get_norm_path ( root , path ), human , digits = 1 )
84
+ print_line ( total , get_norm_path ( path , path ), human )
82
85
return True
83
86
87
+ def get_norm_path (path : str , base : str ) -> str :
88
+ npath = os .path .normpath (path )
89
+ bnpath = os .path .normpath (base )
90
+ bbasename = os .path .basename (bnpath )
91
+ rel = os .path .relpath (npath , bnpath )
92
+ if rel == '.' :
93
+ return bbasename
94
+ return os .path .join (bbasename , rel )
95
+
96
+ def print_line (size , path , human , digits = None ):
97
+ hsize = size_to_str (size , human = human , digits = digits )
98
+ if not human :
99
+ print (f'{ hsize :<10} { path } ' )
100
+ return
101
+ print (f'{ hsize :<10} { path } ' )
84
102
85
103
if __name__ == "__main__" :
86
104
parser = argparse .ArgumentParser (prog = NAME ,
0 commit comments