1. 程式人生 > >python獲取資料夾的大小(即取出所有檔案計算大小)

python獲取資料夾的大小(即取出所有檔案計算大小)

import os

path = r'/Users/authurchen/PycharmProjects/Demo'

# print(os.listdir(path))

ls = os.listdir(path)
files = []

def get_all_file(dir_path):
global files
for filepath in os.listdir(dir_path):
tmp_path = os.path.join(dir_path,filepath)
if os.path.isdir(tmp_path):
get_all_file(tmp_path)
else:
files.append(tmp_path)
return files

def calc_files_size(files_path):
files_size = 0
for f in files_path:
files_size += os.path.getsize(f)
return files_size



files = get_all_file(path)
print(calc_files_size(files))