# ### 計算任意資料夾的大小import os
'''pathvar = "/mnt/hgfs/gongxiang_16/day17/ceshi100"
lst = os.listdir(pathvar)
print(lst)
# 遍歷所有的檔案和資料夾,計算檔案的大小
size = 0
for i in lst:
# print(i)
path_new = os.path.join(pathvar,i)
# print(path_new)
if os.path.isdir(path_new):
print(i,"是乙個[目錄]")
elif os.path.isfile(path_new):
print(i,"是乙個[檔案]")
size += os.path.getsize(path_new)
print(size) # 2132 + 97 = 2229
'''# 使用遞迴來完成資料夾大小的計算
pathvar = "/mnt/hgfs/jiadiannao/ceshi010"
def getallsize(pathvar):
size = 0
lst =os.listdir(pathvar)
print(lst)
for i in lst:
print(i)
# 拼接路徑 + 檔名 => 新路徑
path_new = os.path.join(pathvar,i)
if os.path.isdir(path_new):
size += getallsize(path_new)
'''size = size + getallsize(path_new)
size = 2229 + getallsize(path_new)
'''elif os.path.isfile(path_new):
size += os.path.getsize(path_new)
return size
res = getallsize(pathvar)
# print(res)
# x 2229 + 1272 + 1283 = 4784
計算資料夾大小
下面分享給大家2個封裝好的計算快取大小的方法 如 nsstring cachespath nssearchpathfordirectoriesindomains nscachesdirectory nsuserdomainmask yes lastobject 是沙盒資料夾中 lidrary中的ca...
計算資料夾的大小
沒有直接的api函式,只有採用遞迴方法,下面這種判斷是否是資料夾還是檔案的方法與 刪除資料夾 日誌中的方法不一樣,但是原理一樣。longlong llfoldersize 全域性變數 cstring operatexml calculatefoldersize cstring strfolderpa...
PowerShell 計算資料夾大小
powershell 2.0版本下的指令碼 1 2 3 4 5 6 7 8 9 10 11 12 script function get size of folder script editor snail yu directories get childitemc windows where fo...