python - Computing folder size: Empty folders have a default size? -


i'm trying compute folder's size in python, have strange result.

this snippet of code:

def bestsize(filepath):     """ return tuple 3 values. first file (or folder size). second , third     have sense folder , number of files , subdirectories in folder     """     os.path import getsize, isdir     if not(isdir(filepath)): return (getsize(filepath), 1, 0)     else:         lf = []         ld = []         root, dirs, files in os.walk(filepath):             name in files: lf.append(os.path.join(root, name))             dir in dirs: ld.append(os.path.join(root, dir))         return (sum(getsize(i) in lf), len(lf), len(ld)) 

i have made tests on comparing result said windows's explorer.

i have created folder named "temp", , in subfolder called temp , file of 7 bytes called ciao.txt. temp folder empty. if execute function obtain main folder 7 bytes in size. windows explorer obtain 4096 bytes.

must compute default size all, empty, subfolders?

the default function getsize in os module returns 0 directories.

edit: have tested code on ntfs file system partition

edit: thanks, have understood. better dir/ls command. use former sum computed using getsize, have understood difference fine me.

edit2: have edited code putting last version.

there 2 different ways count size of file.

you can count number of bytes used file.

alternatively, can count number of bytes reserved file. since have use whole blocks @ time, if disk block size 4096 bytes, smallest file uses 4096 bytes no other file can use (unless you're using compressed filesystem option nobody uses anymore).

windows explorer showing latter "size on disk". you're calculating former getsize.


so, if want actual size on disk?

on up-to-date unix , unix-like platforms, os.stat include st_blocks, , python show you. can multiply block size of filesystem right answer. windows doesn't have that.

as quick hack, can round nearest block size. there uncommon cases give wrong answer (e.g., if use ntfs multiple-stream files, you'd have round each stream's size, not total), enough.

finally, can skip os.stat , go right getfileinformationbyhandleex (via ctypes or win32api), or older functions replaced, file_standard_info. allocationsize "size on disk", , endoffile is, normal files, "size".


Comments